AQS core principle

AQS series

1. AQS core principle
2. ReentrantLock principle and example
3. CountDownLatch / Semaphore example and usage scenario
4. BlockingQueue example and usage scenario

1. What is AQS?

The full name of AQS is AbstractQueuedSynchronizer (abstract correct synchronizer), which is an abstract class under the juc ( java.util.concurrent ) package. The core of Java concurrent programming is under this package, and most synchronizer implementations revolve around the common Basic behaviors, such as: waiting queue, conditional queue, exclusive acquisition, shared queue, etc., and this behavior is based on AbstractQueuedSynchronizer . AQS is a synchronizer framework that defines a set of multi-threaded access to shared resources. It is also a state- dependent ) of the synchronizer.

Two, AQS characteristics

  • Blocking waiting queue : AQS internally forms a queue through a Node class, whether it is a conditional queue or a CLH waiting queue.
  • Shared/exclusive : Define shared mode and exclusive mode in the Node class; only one thread can execute in exclusive mode, such as ReentrantLock, and multiple threads can execute simultaneously in shared mode, such as Semaphore and CountDownLatch.
  • Fair/Unfair : You can set whether to compete for resources fairly or unfairly.
  • Reentrant : Because the state is marked by state, it can be locked repeatedly.
  • Allow interruption : AQS determines whether the current thread is in an interrupted state.

3. AQS internal maintenance state

As mentioned earlier, AQS actually relies on an attribute member state to maintain the state internally. The definition of state is such a volatile int state, which is modified by volatile to ensure that each thread can perceive the change of state value in time when the state changes. ;
state indicates the available state of the resource, providing three access methods:

  • getState() : Get the value of the state, if it is 0, the resource is available.
  • setState() : Set the state value, which is equivalent to locking.
  • compareAndSetState() : compare and replace, atomic operation.

4. Queue

4.1 Synchronous waiting queue

The synchronous waiting queue in AQS is also called CLH queue. The CLH queue is a queue based on a doubly linked list structure invented by Craig, Landing, and Hagersten. It is a FIFO (first-in, first-out) waiting queue.
CLH queue

4.2 Conditional waiting queue

Condition is a tool class for coordinating communication between multiple threads. When one or some threads wait for a certain condition to be established, only when the condition is established, these threads will be awakened to re-compete for the lock.
condition queue
Schematic flow chart

5. Summary

AQS was written by Doug Lea . It is a multi-threaded framework based on state and queue. Many implementations are based on AQS. For example, ReentrantLock is executed in an exclusive way, and only one thread is executed at a time. Semaphore and CountDownLatch are both It is executed in a shared manner. Every time there are multiple threads executing at the same time, I always like to use them when doing data push work, and they are very efficient.
This article briefly introduces the principle of AQS . As for the source code, read it slowly. It is true that the logic is very complicated and difficult to understand. Learn to use it first and then read the source code.

Yesterday was 2022-12-31, today is 2023-01-01, happy new year everyone ^_^! ! !

Guess you like

Origin blog.csdn.net/qq_19283249/article/details/128503853