SYNCHRONIZED User experience

zhaozj2021-02-16  55

After using the synchronized keyword modification method, the program will determine whether this method can be called based on the lock of the object that calls this method.

For a class of instance method, then when this method is called by a thread, the other thread cannot call this method by the same object (which can be called by another object of this class).

For a class's StaticMethod, when a thread calls this method through a class object, other threads cannot call this method again through class objects. Since the class object is created by the virtual machine during class load, there is only one, so this method can only be called by one thread.

In the servlet program, the container only instantiates a servlet object, and multiple users access the same servlet object, so synchronous modifications to the servlet method can prevent multiple users from calling simultaneously from simultaneously to avoid sharing conflicts.

When you create a multi-thread program, when you call a class of instance methods in the sub-thread, you should create this object at the main thread, and the reference to the object through the constructor or other interface method is incorporated into the sub-thread. Thread use.

转载请注明原文地址:https://www.9cbs.com/read-21301.html

New Post(0)