Java virtual machines at the operating system level will do everything possible at the virtual machine level to solve the competition, to avoid real competition. At the same time, in the less competitive situation, will also try to eliminate unnecessary competition. Ways to implement these methods include: bias lock, lightweight lock, spin lock, lock cancellation, lock expansion, etc.
1. Bias Lock
The biased lock is a kind of lock optimization method proposed by JDK1.6. The core idea is that if the program does not compete, it cancels the thread synchronization operation that has acquired the lock before. In other words, if a lock is acquired by a thread, it enters the bias mode, and when the thread requests the lock again, no related synchronization is required, thus saving the operation time. If a lock request is made between the other threads, the lock exits bias mode. Use-xx:+usebiasedlocking in the JVM to set enable biased locking.
Biased lock in the lock competition is not too strong to optimize the effect of the situation.
2. Lightweight lock
If the bias lock fails, the Java virtual opportunity allows the thread to request a lightweight lock. Lightweight locks inside the virtual machine, using an object implementation called Basicobjeclock, which consists of a Basiclock object and a Java object pointer holding the lock. The Basicobjectlock object is placed in the stack frame of the Java stack.
First, Basiclock has backed up the original object's Mark Word through the Set_displaced_header () method. Next, using the CAS operation, try to copy the address of Basiclock to the object header of Mark Word. If the replication succeeds, then the lock succeeds. If the lock fails, then the lightweight lock may be inflated to a heavyweight lock.
3. Lock expansion
When a lightweight lock fails, it expands into a heavyweight lock. After a lightweight lock-lock fails, the virtual opportunity performs the following actions: 1. Discards the object header information for the previous Basiclock backup. 2. Through the inflate () method for the lock expansion, the target is to obtain the Objectmonitor 3. Use the Enter () method to try to enter the lock.
4. Spin Lock
After the lock expands, entering Objectmonitor's Enter (), the pedigree is likely to be suspended at the operating system level, so that the performance loss of the thread context switch is relatively large. Therefore, after the lock expands, the virtual opportunity makes the final fight, hoping that the thread can enter the critical section as soon as possible and avoid being suspended by the operating system. A more effective means is to use a spin lock.
A spin lock allows a thread to not be suspended while not acquiring a lock, and to go to an empty loop (known as a spin), and if the thread can acquire a lock after 10 empty loops, continue to entertain. If the lock is still not available, it will be suspended. Not very suitable for lock-competitive occasions.
In JDK 1.6, the Java Virtual machine provides the-xx:+usespinning parameter to account for the spin lock, using the-xx:preblockspin parameter to set the number of times the spin lock waits.
In JDK1.7, the spin lock parameter is canceled. Spin locks are always executed and spin times are self-adjusting by the virtual machine.
5. Lock Removal
Lock Elimination is a Java virtual machine in the JIT compile, across the running context of the scan, the use of escape technology, eliminate the possibility of a shared resource competition lock. Like what:
Public void somebusiness () { ... New StringBuffer (); Sb.append ("XXX"); ...... }
The methods in StringBuffer are synchronized, but this is only a local variable within the method, and there is no possibility of lock contention.
Escape analysis and lock removal can be turned on using parameters-xx:+doescapeanalysis and-xx:+eliminatelocks respectively (lock cancellation must work in-server mode)
In-depth understanding of Java Virtual Machine Note 04-Concurrent lock