AQS
AQS, the queue synchronizer abstractqueuedsynchronizer (hereinafter referred to as synchronizer), is the basic framework used to build locks or other synchronization components.
AQS uses an int member variable to represent the synchronization status.
AQS uses the built-in FIFO bidirectional queue to complete the queuing work of obtaining lock threads
The synchronizer contains two node types of applications, one pointing to the head node and the other pointing to the tail node. The thread that does not obtain the lock will create a node thread safe (compareandsettail) to join the tail of the queue. The synchronization queue follows FIFO, and the head node is the node that successfully obtains the synchronization status.
The thread that does not acquire the lock will create a node and set it to the tail node. As shown in the figure below:
When the thread of the first node releases the lock, it will wake up the successor node. The subsequent node will set itself as the first node when the lock is obtained successfully. As shown in the figure below:
Exclusive / shared lock acquisition
exclusive
Each node spins to observe whether its previous node is a header node. If so, try to obtain the lock.
Exclusive lock acquisition process:
Shared:
The difference between shared and exclusive:
Shared lock acquisition process: