From AtomicInteger to the principle of AQS

What is AtomicIntger

  1. AtomicIntger package is a type of int, it provides a method of atomic increment, decrement atomic assignment method, and a method of atoms, i.e., to provide atomic access and update operations. Which ensures the visibility of the volatile memory (volatile here in Java memory model and volatile, synchronized inside said before, do not know can jump over to see), to achieve its atomic operations are based CAS (compare-and-swap) technology.
  2. The so-called CAS, is to get the value of a flag, stored, and then use CAS instruction attempts to update. If the current flag value unchanged, on behalf of no other threads concurrently modify this value, then successfully updated. Otherwise, it may appear different choices, either to try again, or to return a success or failure result.
  3. As it can be seen from the interior of the AtomicInteger properties, some of which depend on the underlying capabilities provided Unsafe, low-level operations; volatile in the value field, the value recorded in order to ensure visibility.
  4. CAS is the basis for Java Concurrency in the so-called lock-free mechanism.

AQS

Principles and Applications need to understand why AQS, how to use AQS, at least what to do, combined with further practice JDK source code, understand the AQS

What is the AQS

AQS AbstactQueuedSynchronizer is short, it is a Java increased level synchronization tools, with a variable of type int represents --state synchronization state, and provides a series of CAS operations to manage the synchronized state. AQS main role is to provide a unified Java Concurrency synchronization support the underlying component usage by inheritance AQS achieve its template method, then the sub-class as an inner class synchronization components. For example ReentrantLock inside subclass Sync:

 public class ReentrantLock implements Lock, java.io.Serializable {
    private static final long serialVersionUID = 7373984872572414699L;
    private final Sync sync;

    abstract static class Sync extends AbstractQueuedSynchronizer {
        ...
    }
}
复制代码

AQS internal data and methods, may be simply split into:

  1. A volatile state characterized integer members, while providing a method setState and getState
private volatile int state;
复制代码
  1. A first-in first-out (FIFO) queue of waiting threads to realize multi-thread between competition and wait, which is one of the core AQS mechanism.
//内部类Node
static final class Node
复制代码
  1. CAS based on various methods of operation, and acquire / release specific method to achieve the desired synchronization of the various structures.
//基于CAS的基础操作方法
private final boolean compareAndSetHead(Node update) 
private final boolean compareAndSetTail(Node expect, Node update) 
private static final boolean compareAndSetWaitStatus(Node node,int expect,int update) 
private static final boolean compareAndSetNext(Node node, Node expect, Node update) 

//实现的acquire/release方法等等
public final void acquire(int arg) 
public final boolean release(int arg)
······
复制代码
  1. To use AQS achieve a synchronous structure, to achieve at least two basic types of methods, namely acquire steps to obtain the exclusive rights to resources; there is the release operation, the release exclusive on a resource.

Guess you like

Origin juejin.im/post/5d6a0675f265da03eb13e0cf