What is a AtomicIntger

  1. AtomicIntger is a wrapper around int that provides atom-increment, atom-decrement, and atom-assignment methods that provide atomic-access and update operations. Volatile guarantees memory visibility, and atomic operations are implemented based on CAS (compare-and-swap) technology.
  2. The so-called CAS is to obtain the flag bit of a certain value, store it, and then use CAS instruction to try to update it. If the current flag bit value has not changed, it means that no other threads have concurrently modified the value, and the update is successful. Otherwise, a different choice may arise, either to retry or to return a success or failure result.
  3. As you can see from the internal attributes of AtomicInteger, it relies on some of the underlying capabilities that Unsafe provides for low-level operations; Use the volatile value field to record the value for visibility.
  4. CAS is the basis for the so-called lock-free mechanism in Java concurrency.

AQS

Understand why AQS is needed, how to use AQS, at least what to do, and then further combine the practice in JDK source code to understand the principle and application of AQS

What is the AQS

AQS is short for AbstactQueuedSynchronizer, it is a Java improve the bottom of the synchronous tools, with a variable of type int, said the state synchronization state, and provides a series of CAS operation to manage the synchronization state. The main purpose of AQS is to provide uniform underlying support for concurrent synchronous components in Java by inheriting AQS to implement its template methods and then using subclasses as internal classes for synchronous components. For example, Sync, a subclass of ReentrantLock:

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

    abstract static class Sync extends AbstractQueuedSynchronizer {... }}Copy the code

AQS internal data and methods can be simply divided into:

  1. A volatile integer member represents state and provides both setState and getState methods
private volatile int state;
Copy the code
  1. A first in, first out (FIFO) queue of waiting threads to achieve inter-threaded competition and waiting, which is one of the core of the AQS mechanism.
// Inner class Node
static final class Node
Copy the code
  1. Various basic cas-based operation methods, as well as various acquire/release methods that specific synchronization structures are expected to implement.
// Basic operation method based on 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) 

// Implement acquire/release methods etc
public final void acquire(int arg) 
public final boolean release(int arg)· · · · · ·Copy the code
  1. To use AQS to realize a synchronous structure, at least two basic types of methods should be implemented, namely acquire operation, to obtain exclusive rights of resources; There is also the release operation, which releases the exclusive possession of a resource.