[Multithreading]-Introduction to AQS

1. Introduction to AQS

1. What is AQS

AQS is the abbreviation of AbstractQueuedSynchronizer, which is translated into abstract synchronization queue in Chinese. It is the basic component to realize the synchronizer. Most of the locks in JUC (Java.util.cuncurrent package) are implemented by AQS design. In most cases, we may You will rarely have access to AQS, but understanding the principles of AQS will help us understand the lock mechanism in Java.

2. Implementation of AQS class

In order to facilitate understanding, let's first paste the class diagram of AQS (hereinafter AbstractQueuedSynchronizer is all referred to as AQS): Insert picture description here
The main variables in AQS are actually divided into two types:

  1. One is the state used to maintain a single state of information.It can be used to indicate the number of reentrant locks acquired by the current thread, or the number of locks acquired by the read lock in the read-write lock.
  2. Another node-related variable is actually used to maintain a FIFO (first in first out) two-way queue, which internally records the head and tail of the queue through the node head and tail.

For specific AQS design ideas, we can refer to a simple flow chart: the
Insert picture description here
following enters the link of reading pictures and writing articles:

  1. I believe many people have discovered that the process of AQS is very similar to the process of acquiring a lock. This is> Because the main function of AQS is to realize different synchronization locks.
  2. When the state field in the AQS class is 0, it indicates that the current lock is in a holdable state. When the lock> is held by a thread, the state field will be +1, and each reentry will be +1. When the lock is released, state The field will be set to 0.
  3. A CLH queue is mentioned here. In fact, the CLH queue is an abbreviation of raig, Landin and Hagersten. It is mainly a two-way queue with FIFO characteristics.

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/111880774
AQS
AQS