Take exclusive unfair lock as an example, through 5 threads to compete for Reentrantlock process, plot Reentrantlock source code realization, understand the work flow of display lock.
There is only one thread that gets the lock at any moment, the thread that does not get the lock is packaged as a node, and the node is then placed in the synchronization queue from the end of the queue via the CAS spin.
Increase the tail node why use CAS because there will be multiple threads competing for tail nodes.
The above is the process of thread competition and all the things that need to be done after the competition fails.
The above is the whole process of thread acquisition and release lock, which can be summarized by the following flowchart.
In addition, the above source code are from jdk1.8, this article as a sort of ideas, only a summary of the key parts, detailed process please readers follow the idea of self-reading source code, English clearance is one of the necessary skills to read, or can not read the comments really hurt the egg. The important thing to say three times, English must be better!
English must be better! English must be better!
Graphical source of Java Lock Acquisition and release (AQS) Chapter