Java Concurrency Art (4) lock state and contrast

Lock escalation

Java SE1.6 order to obtain and release locks reduce performance brought consumption, the introduction of the "biased locking" and "lightweight lock" in the Java
SE 1.6, the lock in a total of four state-level from low to high again is: nothing state, tend to lock status, lock status lightweight and heavyweight lock state, these states will compete with the escalating situation. Lock can upgrade but can not downgrade means biased into a lightweight lock after lock escalation hungry can downgrade to bias lock. This lock upgrade but can not downgrade the book Luo, the purpose is to improve the efficiency of obtaining and release locks

Biased locking

In most cases, there is no multi-threaded lock not only competitive, but always obtained from the same thread several times, in order to allow the thread to acquire a lock and lower the cost of introducing a biased locking.
When a thread to acquire the lock and access the sync block, will lock the recording head and the object in the stack frame store lock bias thread ID, after the thread does not require a CAS in sync blocks entry and exit to lock and unlock , simply to test the head of Mark Word objects in memory if the current thread pointing to bias lock.
If the test is successful, it indicates that the thread has acquired the lock.
If the test fails, you will need to re-test Mark Word in biased locking identity is set to 1 (indicating the current is biased lock):
If not set, the CAS compete lock;
if set, try using CAS object head biased locking points to the current thread.
Here Insert Picture Description
Biased locking in Java6 and in Java7 is enabled by default, but it is only activated after the application starts a few seconds. JVM may be used if necessary to close the delay parameter -XX :: BiasedLockingStartupDelay = 0.

If you determine the application in all the locks in a competitive state under normal circumstances, can be turned off by biased locking JVM parameters: -XX: - UseBiasedLocking = false, then the program goes into default lightweight lock state.

Lightweight lock

(1) Lightweight locking
thread prior to performing the sync blocks, the JVM will create space for storing in the stack frame of the locks in the current thread and the object header Mark Word copied into the lock record.
The thread then attempt to use the CAS object Mark Word replacement head pointer to lock be recorded. If successful, the current thread to acquire the lock, and if that fails, represents another thread lock contention, they try to use the spin current thread to acquire the lock
(2) Lightweight unlocked
when unlocking lightweight, use of atomic CAS operation Displaced Mark Word replace head back to the object, if successful, it means that there is no competition occurs. If it fails, there is competition represents the current lock, the lock will be expanded into a heavyweight lock
Here Insert Picture Description
because the spin will consume CPU, in order to avoid unwanted spin (such as to acquire a lock thread is blocked), once heavyweight lock upgrade to lock it It will not be restored to the state lightweight lock. When the lock is in this state, other threads view of acquiring the lock will be blocked to live, when the thread releases the lock that holds the lock will wake up these threads, the thread will be awakened a new round of wins battle lock

Comparison of the advantages and disadvantages lock

lock advantage Shortcoming Applicable scene
Biased locking Locking and unlocking no additional consumption, there is a difference nanosecond and execute asynchronous method compared to only If there is lock contention between threads, it will bring additional lock revocation consumption It applies to only one thread to access the scene sync blocks
Lightweight lock Thread does not block competition, improve the response speed of the procedure If still unable to get a lock competing threads, using spin will consume CPU Performing seek response time is very fast sync blocks
Heavyweight lock Competition is not using spin thread. It does not consume CPU Thread is blocked, causes the thread from user mode to kernel conversion units, consumption of resources Seek a certain sync block long execution speed
Published 24 original articles · won praise 1 · views 547

Guess you like

Origin blog.csdn.net/qq_45366515/article/details/105126691