[Java concurrent programming practice] ----- "J. U. C": AQS analysis (1)
Refer to the built-in locks of Java. For the JUC synchronizator, it should have two basic functions: Getting locks and Releasing locks. To obtain the lock, you must first determine whether the current status can be obtained. If the lock cannot be obtained, the lock is blocked and the lock is released and modified, let other threads get the lock (wake up other threads) as follows:
Lock: while (state) {// getLock (); // get the lock return currentThread; // return the current thread} release: updateState (); // modify the status of y other thread; // wake up other threads
As we know, in JUC, the methods for obtaining and Releasing locks by different syncs are different, for example: lock. lock (), Semaphore. acquire (), CountDownLatch. await () and so on. If they all implement their respective methods, the JUC framework can only be regarded as a middle-and lower-level framework design. This is the emergence of AQS. AQS, as a core processing framework, provides a large number of synchronization operations. At the same time, users can also customize them to implement their own synchronization. The main framework is as follows:
It can be seen that AQS is the cornerstone of JUC synchronization. The following briefly describes the technical principles of AQS and will describe it in detail later.
1. Status bit state
AQS uses a 32-bit integer to indicate the synchronization status. It is modified with volatile:
private volatile int state;
In the mutex lock, it indicates whether the thread has obtained the lock, 0 has not been obtained, 1 has been obtained, and more than 1 indicates the number of retries. At the same time, AQS provides the getState (), setState (), and compareAndSetState () methods to obtain and modify the value:
protected final int getState() { return state; } protected final void setState(int newState) { state = newState; } protected final boolean compareAndSetState(int expect, int update) { return unsafe.compareAndSwapInt(this, stateOffset, expect, update); }
These methods require the support of the java. util. concurrent. atomic package and use CAS to ensure its principle and visibility.
2. CLH synchronization queue
As mentioned above, AQS maintains a fifo clh queue internally, so AQS does not support priority-based synchronization policies. The reason for choosing a CLH queue is that the CLH lock is easier to process cancel and timeout than the MSC lock, at the same time, it is very easy for him to get in and out of the queue fast, everything is unobstructed, and check whether there is a thread waiting (head! = Tail, different headers and tails ). Of course, compared with the original CLH queue lock, ASQ adopts a variant CLH queue lock:
1. The locked spin used by the original CLH, while the CLH of AQS uses a state field in each node to control blocking, rather than spin.
2. To handle timeout and cancel operations, each node maintains a pointer pointing to the front-end. If a node's precursor is cancel, the node can move forward using the frontend Status field.
3. The head node uses a slave node.
3. Shared locks and mutex locks
In the CLH queue lock maintained by AQS, each Node represents a thread to obtain the lock. The Node has two constants SHARE and EXCLUSIVE. Here, SHARE represents the sharing mode, and EXCLUSIVE represents the EXCLUSIVE mode.
static final class Node { /** Marker to indicate a node is waiting in shared mode */ static final Node SHARED = new Node(); /** Marker to indicate a node is waiting in exclusive mode */ static final Node EXCLUSIVE = null; ///////// }
The sharing mode allows multiple threads to obtain the same lock. In the exclusive mode, a lock can only be held by one thread, and other threads must wait.
4. Blocking and Wakeup
We know that when using Java built-in locks, we can use wait and Y methods to block and wake up threads. However, AQS does not adopt this mode, but uses LockSupport. park () and LockSupport. unpark () local method to implement thread blocking and wakeup.