Java bias Lock (biased Locking) is a multithreaded optimization introduced in Java 6. It further improves the running performance of the program by eliminating the synchronization primitives in the case of non-competitive resources.
Lightweight locking is also a multi-threaded optimization, which differs from biased locking in that lightweight locks are passed through CAs to avoid costly mutex operations, while biased locking is a complete elimination of synchronization in a non-competitive scenario, even CAS is not executed (CAS itself is still an operating system synchronization primitive, Always go back and forth between the JVM and the OS for a certain amount of overhead).
The so-called non-competitive scenario, for example, is a single-threaded access to a resource or method with synchronization.
Principle of biased lock implementation
A bias lock, as the name implies, is biased towards the first thread that accesses the lock.
If the lock is not accessed by another thread during the next run, the thread holding the biased lock will never need to trigger the synchronization.
If another thread preemption lock is encountered during the run, the thread holding the biased lock is suspended, and the JVM tries to eliminate the biased lock on it and restore the lock to a standard lightweight lock. (A bias lock can only work on a single thread).
When the lock object is fetched by the thread for the first time, the virtual opportunity sets the flag bit in the Mardword of the object's head to 01, which is the bias mode. The ID of the thread is present in Markword, and after success, the thread holding the lock does not perform any synchronization operations. However, when another thread attempts to acquire the lock, this bias mode declares the end
。
Deepen reading;
Deep JVM lock Mechanism 1-synchronized
Deep JVM lock Mechanism 2-lock
java--bias Lock