JVM learning (IX): Lock optimization

1, high concurrency is a major improvement to the JDK1.5 JDK1.6, the emergence of adaptive spin (Adaptive Spinning), locks elimination (Lock Elimination), lock coarsening (Lock Coarsening), lightweight lock (Lightweight Locking ) and biased locking (biased locking) and a series of locks optimization techniques.

 

2, adaptive spin lock spin:

(1) spin-locks : exclusive synchronize the greatest impact on performance is achieved blocked into kernel mode needs to complete, due to the usually shared data lock status will only last a short period of time, so that if a thread is waiting for acquiring the lock when more later, CPU-execution time but not block, execute a busy loop (spin), which implements spin locks.

(2) has been introduced in spin lock JDK1.4.2, the spin not replace blocked , because the processor time required spin, spin waiting time must be limited, the number is 10. The default spin parameters may be used - XX: PreBlockSpin settings.

(3) adaptive Spin : JDK1.6 introduced adaptive spin lock, i.e., spin time is no longer fixed, but a state of a front in the same time and a spin lock owner of the lock to decide. If it is determined that the spin lock acquired probability of success is large, wait longer allowed to spin, otherwise may go directly to clog omitted spin process, to avoid waste of processor resources.

 

3, locks elimination : the virtual machine time compiler at run time, some for the code synchronization, but it is impossible to detect the presence of shared data locks to eliminate competition, without having to synchronize directly executed . For example, before stitching JDK1.5 String class with a thread-safe StirngBuffer.append () method, but due to the String class is immutable, so it will be after the lock cancellation operation code is compiled.

 

4, lock coarsening : the virtual machine to detect if there is a series of successive operations is repeated locking and unlocking of the same object , the lock will range extension synchronization (roughening) to the outside of the entire sequence of operations, so that only lock it once.

 

5, lightweight lock : By using CAS operation to avoid the overhead of synchronizing mutually exclusive, in no multithreading competition under the premise, can reduce the performance overhead of traditional heavyweight lock generated.

 

6, biased locking : in the absence of competition in the case of the whole synchronization are eliminated, the virtual machine by parameters -XX: + UseBiasedLocking (JDK1.6 default) settings.

 

7, the lock flag stored in the HotSpot VM object header in (Mark Word):

Memory contents Flag status
The hash code of the object, the object generational Age 01 Unlocked
Pointers to lock records 00 Lightweight lock
Heavyweight locks pointer pointing 10 Expansion ( heavyweight lock )
Empty, no need to record 11 GC mark
Bias thread ID, timestamp bias, the object generational Age 01 Be biased


8, biased locking compete the revocation of bias can be converted to lock lightweight, lightweight lock can be expanded upgraded to heavyweight lock .

 

Guess you like

Origin www.cnblogs.com/xy80hou/p/11407365.html