AQS and principle

AQS: AbstractQuenedSynchronizer abstract queue type synchronizer. Is a synchronization framework, it provides a common mechanism to atomically manage the synchronization state, block and wake up threads, and maintain the queue of blocked threads.

AQS is widely used in JDK 6. Synchronizers based on AQS include: ReentrantLock, Semaphore, ReentrantReadWriteLock,
CountDownLatch and FutureTask


The core idea of AQS is that if the requested shared resource is free, the thread currently requesting the resource is set as a valid worker thread, and the shared resource is set to a locked state.

If the requested shared resource is occupied, then a mechanism for thread blocking and waiting and lock allocation when awakened is required. This mechanism AQS is implemented with CLH queue locks, that is, threads that cannot obtain locks temporarily are added to the queue.
The CLH (Craig, Landin, and Hagersten) queue is a virtual two-way queue. A virtual two-way queue means that there is no queue instance, only the relationship between nodes.

The design pattern is the template pattern.
Core data structure: doubly linked list + state (lock state). The
underlying operation: CAS

CAS, Compare and Swap
The idea of ​​CAS is very simple: three parameters, a current memory value V, the old expected value A, the value to be updated B, if and only if the expected value A and the memory value V are the same, the memory value Change to B and return true, otherwise do nothing and return false.
The CAS operation in the JVM is implemented using the CMPXCHG instruction provided by the mentioned processor; the basic idea of ​​cyclic CAS implementation is to cyclically perform the CAS operation until it succeeds;

 

 

public final void acquire(int arg) {
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        selfInterrupt();
}

The state value of int type in AQS, here is to modify the value of state through CAS (optimistic lock). The basic operation of lock is achieved through optimistic locking.
Acquiring the lock through CAS, then the lock is not acquired, how to wait for the lock to be acquired? We can look at the logic of the else branch, the acquire method:

  1. tryAcquire: Will try to acquire a lock through CAS again.
  2. addWaiter: Add the current thread to the doubly linked list (waiting queue) locked above by spinning CAS.
  3. acquireQueued: Determine whether the current queue node can acquire the lock through spin.

It can be seen that when the current thread reaches the head, it tries CAS to update the lock state. If the update is successful, it means that the waiting thread is successfully acquired. Remove from the head.
It can be basically confirmed that releasing the lock is to modify the state value State in AQS. At the same time, the thread waiting node in the next linked list is updated.

 

Get lock process

Can refer to:

https://www.cnblogs.com/weechang/p/12545271.html


 

Guess you like

Origin blog.csdn.net/m0_37541228/article/details/114264929