Java multi-thread lock upgrades
Let's talk about why there's a lock upgrade.
Because the sycronized is a heavyweight lock (also a pessimistic lock), every time a request for a lock is made, if the current resource is occupied by another thread, the current thread block is added to the blocking queue, and then the cache of the front thread is cleared. Wait until the lock is released and then wake the current thread through notify or notifyall and put it in a ready state. Such a thread switching back and forth is very consuming system resources, and sometimes, the line isconcurrently suspend the resource is released. The Java thread is mapped to the operating system of the native thread every time the blocking or awakening of the thread through the user state to the nuclear mentality or nuclear mentality to the User state transformation, this is a waste of resources, resulting in performance degradation, so the JVM optimizes sychronized, The sycronized is divided into three types of locks: bias locks, lightweight locks, and heavyweight locks.
A lot of times, for a possible concurrent access to the object, in fact, is rarely competitive, even if some resources are competitive and in a very small amount of time the resources will be released, and in this case the thread hangs is very wasteful performance.
Bias Lock (Optimistic lock):
When the lock object is first retrieved by the thread, the virtual opportunity sets the lock flag bit in the object header of the lock object to 01 and sets the bias lock flag to 1, and the thread places its own ID value into the object header through CAS (because there may be other threads competing for the lock in the process). So in the way of CAs, if there is competition, it will be upgraded to lightweight locks, if the successful thread gets the lightweight lock. So that each time you enter the lock object without any synchronization operations, directly compare the current lock object header is not the ID of the thread, if it can be entered directly.
Bias lock upgrade to lightweight lock
A bias lock is a non competitive lock that, in most cases, will be upgraded to a lightweight lock if there is competition. Now let's assume that thread 1 holds a bias lock, and that the threads and the competing bias locks go through the following processes:
1. Thread 2 first checks for the bias lock tag, and if it is 1, indicating that the current is a bias lock, then the JVM will find thread 1 to see if thread 1 still survives 2
2. If thread 1 has been executed, that is, thread 1 does not exist (thread 1 is not going to release the biased lock), then the bias lock to 0, the object header set to become unlocked state, in the way of CAs to try to put the ID of thread 2 into the object header, do not lock escalation, or bias lock
3. If thread 1 is still alive, first suspend thread 1, turn the lock flag bit to 00 (Lightweight lock) and then carve out a space in the stack frame of thread 1 (Display mark Word) to replace the object header's mark Word with the line Cheng stack frame. A pointer to the current line Cheng is stored at this time in the object header. It becomes a lightweight lock at this point. Continue with thread 1, then thread 2 tries to acquire the lock in the form of CAs.
The biggest difference between lightweight locks and bias locks
The difference between lightweight and biased locks lies in the fact that lightweight locks are used to get lock objects in a way that is synchronous with CAs and that the entire synchronization process is canceled.
Lightweight lock (Optimistic lock)
How lightweight locks are created in the above have been talked about, next, the lightweight lock gets the lock object, the lightweight lock tries to acquire the lock object through CAs, or spin, and once the failure is checked, whether the object's header is stored in the pointer to the current line Cheng and, if so, can get the object. If it is not to show that there is competition then it will swell into a heavyweight lock. The unlock of lightweight locks is also an attempt to replace the mark Word of the object header with the display mark Word in the thread in the form of CAs, and if successful, release the lock, if the failure indicates that there are many other threads waiting for the lock (indicating that it is not a lightweight lock but a heavyweight lock), Wakes up these threads and then releases the locks.
Lightweight lock expansion for heavy-weight locks
Once there are more than two threads competing for the lock, the lightweight lock expands to a heavyweight lock, and the lock's state becomes 10, and the object header stores a pointer to a stack frame that points to a heavy lock. and other threads waiting for the lock to enter the blocking state, wait for the heavy lock release to be awakened and then compete.