In Java concurrent programming, the lock mechanism is important to control the use of shared memory between threads. So how does the Java internal lock get implemented?
First, we need to define a concept.
The lock in Java is an object-level concept, in which each object is inherently used as a lock.
The bottom of the implementation, in fact, the lock is in the Java object header in the Markword field, according to the level of the lock, the storage structure is different, but there is a 2bit lock identity bit. Pessimistic lock
Pessimistic lock is synchronize internal implementation mechanism, after Java 1.6, the pessimistic lock was improved, that is, now not all synchronization operations are bound to be blocked. Today's pessimistic locks are divided into biased locks, lightweight locks, and heavyweight locks according to the way Locks are acquired and released. The lock identification bits are 01, 00, 10, respectively. These states will escalate (but not degrade) as the lock becomes competitive (but not degraded) in favor of Locks
Bias locks are a mechanism for releasing locks when competition arises.
Specifically, the head of the object that is the lock holds a biased lock identity that stores the thread ID of the currently acquired lock. Suppose that thread A is currently getting a lock, but thread A does not release the lock after it finishes the sync block, but instead waits until the other threads compete before releasing the lock. Why do you design this way? Because if the next thread A is to access the same synchronization block, then you do not need to update the object header of the lock object by using the CAS operation, which is simpler than the thread ID is equal.
The following is the process of obtaining and releasing the bias Lock:
A supplementary explanation of the above figure:
When competing for a lock, you need to check for threads that hold a biased lock. If the thread is dead, it is unlocked automatically, the thread ID in the Markword of the object header of the lock object is empty, if the thread is currently not using a sync block, unlocks, the Markword thread ID of the lock object is empty, and the thread is restored. At the same time, other threads can set the object ID in the object header Markword of the lock object to its own ID through a CAS operation, that is, to obtain a bias lock. Lightweight Locks
When the withdrawal and acquisition of the preference lock often occurs (that is, the competition for the lock often occurs), it is upgraded to a lightweight lock.
Unlike the process of acquiring a lightweight lock through a CAS, a thread always copies the Markword of the lock object to the stack space and then attempts to update Markword through CAS, if the failure indicates that the other thread successfully updated the Markword ( That is, the lock is acquired by a spin-CAS attempt to regain the lock (always consuming CPU time slices) and if successful in a short time, the lock will not escalate and the lock will be upgraded to a heavyweight lock if the lock is not acquired for a long time. Heavy-weight lock
When a lock on a heavy-level lock fails without a spin in the CAS attempt to regain the lock, it enters a blocking state (a CPU-time slice that is freed by the CPU schedule after the other threads release the lock). Summary
Three kinds of locks are not good or bad, respectively, have their respective applicable occasions:
| Lock |
Advantages |
Disadvantage |
Applicable Scenarios |
| Bias Lock |
In the absence of lock competition, the lock is extremely fast and eliminates the need to unlock |
If more threads compete for locks, it makes the process of unlocking extra time-consuming |
Scene of a long time thread accessing the synchronized block |
| Lightweight locks |
Competitive threads do not block and respond quickly |
If locks are not available for a long period of time, the spin-CAS can incur significant CPU overhead |
The pursuit of response time, and synchronization block execution speed block scene |
| Heavy-weight lock |
The thread is always blocked when the lock is not acquired and the CPU resources are in time |
Thread blocking, the need to restart the lock through the dispatch, slow response |
A scenario that pursues high throughput and that the synchronization block executes for a long time, such as a large number of I/O operations |
optimistic Lock
Optimistic locks allow all threads to create a copy of shared memory without blocking. These threads may then modify their copies and attempt to write their modified versions back to shared memory through the CAS operation. If another thread has modified the shared memory, the thread will have to get a new copy again, make changes on the new copy, and try to write them back into shared memory again through CAs.
The reason to call it "optimistic lock" is that threads get copies of the data they want to modify and make changes, and optimistic assumptions that no other threads have made changes to shared memory during this time, and when this optimistic assumption is established, the thread simply completes the shared memory update without a lock. When this assumption is not true, the work done by the thread is discarded.
Whether optimistic assumptions succeed or fail, optimistic locks do not use locks for concurrent control. Applicable Scenarios
Optimistic locks apply only to shared memory contention is not a very high case.
If the amount of content that needs to be modified on shared memory is so large, it will waste a lot of CPU cycles on re copying and modification because the CAS update shared memory fails. Typical Use Cases
Atomicinteger and so on.