Detailed explanation of synchronized

The synchronized keyword is concise, clear, and semantically clear, so even with the Lock interface, it is still widely used. The semantics of its application layer is that any non-null object can be used as a "lock".
Synchronized relies on the JVM at the software level, and Lock relies on special CPU instructions at the hardware level.
1. How the JVM implements synchronized
There are two built-in synchronized syntaxes in the java language: synchronized statements and synchronized methods.
When the synchronized statement is compiled into bytecode by javac, the monitorenter and monitorexit bytecode instructions are inserted at the entry and exit positions of the synchronized block, respectively.
When a synchronized method is compiled into bytecode by javac, it will be translated into ordinary method call and return instructions such as: invokevirtual, areturn instructions. There is no special instruction at the VM bytecode level to implement the method modified by synchronized, but in In the method table of the Class file, the synchronized flag in the access_flags field of the method is set to 1, indicating that the method is a synchronized method and uses the object that calls the method or the internal object of the JVM to which the method belongs to indicate Klass as the lock object.

2. Hotspot currently implements synchronized
There are currently three types of locks in hotspot to implement the semantics of synchronized. The reason there are three types is because the three types of problems to be solved are different, and the optimizations made are also different. These three types of locks are biased locking, stack lock, and infalted (ObjectMonitor). In terms of simple violence removal, from a lightweight perspective, biased lock is the best, and inflated is the worst.

3. What is synchronized locked
synchronized locks objects rather than functions or code.
When synchronized acts on a method, the object instance (this) is locked; when it acts on a static method, the class instance corresponding to the object is locked, because the Class data exists in the permanent band, so the static method lock is equivalent to A global lock of this class; when synchronized acts on an object instance, the corresponding code block is locked.
Each object has only one lock (Lock) associated with it. When the synchronized statement or function is reached, the lock will be taken by the current thread (thread), and other (thread) will be taken when accessing it again. Suspended before it locks.
In the HotSpot JVM implementation, locks have a special name: object monitors.

4. The usage scenario
of synchronized 1 public synchronized void method1
locks the object, one of the instances of the class, when the object (just this object) executes the synchronized method in different threads, mutual exclusion will be formed between the threads . The synchronization effect is achieved, but if different threads execute this synchronization method on different objects of the class at the same time, mutual exclusion will not be formed between the threads because they own different locks.

2.synchronized(this){ //TODO} The
same

3.public synchronized static void method3
locks this class. When all objects of this class (multiple objects) call this static synchronization method in different threads, the thread Mutual exclusion will be formed between them to achieve synchronization effect, but if multiple threads call method1 and method3 at the same time, they will not cause mutual exclusion. For details, see the last explanation.

4.synchronized(Test.class){ //TODO}
Same as three

5.synchronized(o) {}
The o in this can be any Object object or array, not necessarily its own object or class. Whoever owns the lock of o can operate the block of program code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326223940&siteId=291194637