AQS依赖于提供一个原子变量(state)用来表示当前锁对象的同步状态,并且提供了三个对state变量原子操作的方法
- getState()
- setState()
- compareAndSetState()
/**
* Returns the current value of synchronization state.
* This operation has memory semantics of a {@code volatile} read.
* @return current state value
*/
protected final int getState() {
return state;
}
/**
* Sets the value of synchronization state.
* This operation has memory semantics of a {@code volatile} write.
* @param newState the new state value
*/
protected final void setState(int newState) {
state = newState;
}
/**
* Atomically sets synchronization state to the given updated
* value if the current state value equals the expected value.
* This operation has memory semantics of a {@code volatile} read
* and write.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that the actual
* value was not equal to the expected value.
*/
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
AQS包括同步队列(CLH)、独占模式、共享模式。AQS提供了获取锁、释放锁的顶层方法,其中有些具体的方法需要实现者自己去覆写锁的获取、释放逻辑。
同步队列
一个内部Node组成的FIFO双向队列。当前线程获取同步状态失败时,同步器会将当前线程以及等待状态等信息构造为一个Node并将其加入到同步队列中,同时阻塞当前线程,当同步状态释放时,会把首节点中的线程唤醒,使其再次尝试获取同步状态。
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;
/** waitStatus value to indicate thread has cancelled */
static final int CANCELLED = 1;
/** waitStatus value to indicate successor's thread needs unparking */
static final int SIGNAL = -1;
/** waitStatus value to indicate thread is waiting on condition */
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
static final int PROPAGATE = -3;
/**
* Status field, taking on only the values:
* SIGNAL: The successor of this node is (or will soon be)
* blocked (via park), so the current node must
* unpark its successor when it releases or
* cancels. To avoid races, acquire methods must
* first indicate they need a signal,
* then retry the atomic acquire, and then,
* on failure, block.
* CANCELLED: This node is cancelled due to timeout or interrupt.
* Nodes never leave this state. In particular,
* a thread with cancelled node never again blocks.
* CONDITION: This node is currently on a condition queue.
* It will not be used as a sync queue node
* until transferred, at which time the status
* will be set to 0. (Use of this value here has
* nothing to do with the other uses of the
* field, but simplifies mechanics.)
* PROPAGATE: A releaseShared should be propagated to other
* nodes. This is set (for head node only) in
* doReleaseShared to ensure propagation
* continues, even if other operations have
* since intervened.
* 0: None of the above
*
* The values are arranged numerically to simplify use.
* Non-negative values mean that a node doesn't need to
* signal. So, most code doesn't need to check for particular
* values, just for sign.
*
* The field is initialized to 0 for normal sync nodes, and
* CONDITION for condition nodes. It is modified using CAS
* (or when possible, unconditional volatile writes).
*/
volatile int waitStatus;
/**
* Link to predecessor node that current node/thread relies on
* for checking waitStatus. Assigned during enqueuing, and nulled
* out (for sake of GC) only upon dequeuing. Also, upon
* cancellation of a predecessor, we short-circuit while
* finding a non-cancelled one, which will always exist
* because the head node is never cancelled: A node becomes
* head only as a result of successful acquire. A
* cancelled thread never succeeds in acquiring, and a thread only
* cancels itself, not any other node.
*/
volatile Node prev;
/**
* Link to the successor node that the current node/thread
* unparks upon release. Assigned during enqueuing, adjusted
* when bypassing cancelled predecessors, and nulled out (for
* sake of GC) when dequeued. The enq operation does not
* assign next field of a predecessor until after attachment,
* so seeing a null next field does not necessarily mean that
* node is at end of queue. However, if a next field appears
* to be null, we can scan prev's from the tail to
* double-check. The next field of cancelled nodes is set to
* point to the node itself instead of null, to make life
* easier for isOnSyncQueue.
*/
volatile Node next;
/**
* The thread that enqueued this node. Initialized on
* construction and nulled out after use.
*/
volatile Thread thread;
/**
* Link to next node waiting on condition, or the special
* value SHARED. Because condition queues are accessed only
* when holding in exclusive mode, we just need a simple
* linked queue to hold nodes while they are waiting on
* conditions. They are then transferred to the queue to
* re-acquire. And because conditions can only be exclusive,
* we save a field by using special value to indicate shared
* mode.
*/
Node nextWaiter;
/**
* Returns true if node is waiting in shared mode.
*/
final boolean isShared() {
return nextWaiter == SHARED;
}
/**
* Returns previous node, or throws NullPointerException if null.
* Use when predecessor cannot be null. The null check could
* be elided, but is present to help the VM.
*
* @return the predecessor of this node
*/
final Node predecessor() throws NullPointerException {
Node p = prev;
if (p == null)
throw new NullPointerException();
else
return p;
}
Node() { // Used to establish initial head or SHARED marker
}
Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
}
Node(Thread thread, int waitStatus) { // Used by Condition
this.waitStatus = waitStatus;
this.thread = thread;
}
}
前面还有很大段注释解释了Node的用处。
Node其中的属性值:
1、SHARED和EXCLUSIVE表明了是共享还是独占模式
2、waitStatus的几个状态值:指示是否被取消、指示继任节点需要被唤醒、指示是否在等待条件、指示下个共享获取节点应该无条件传播
3、pre和next分别连接了前后的同步节点,组成了FIFO双向队列
4、存储对应的线程,一个线程对应一个节点
5、nextWaiter用于等待队列,只会出现在独占模式中
独占获取
独占,顾名思义,锁在同一个时刻只能由一个线程占有。独占锁的某些具体实现方法以ReentrantLock举例。
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();//如果线程被打断唤醒过,在上面的方法中没有响应,只是做了中断标记,所以这里需要补一个中断
}
protected boolean tryAcquire(int arg) {
throw new UnsupportedOperationException();
}
独占获取锁的顶层方法,先尝试获取,tryAcquire由各个实现类去覆写,看下ReentrantLock中的实现。
//ReentrantLock中NonfairSync覆写的tryAcquire方法
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();//1、获取当前state
if (c == 0) {//2、如果为0,说明当前锁没被占用
if (compareAndSetState(0, acquires)) {//3、尝试CAS设置state
setExclusiveOwnerThread(current);//4、如果成功,说明成功获取到锁,将独占线程设置为当前线程
return true;
}
}
else if (current == getExclusiveOwnerThread()) {//5、如果占有锁的是线程自己
int nextc = c + acquires;//6、增加占有次数,释放时需要依次释放
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");//7、重入次数溢出
setState(nextc);//8、设置state为新值,因为此时已占有锁,不需要CAS设置
return true;
}
return false;
}
成功则结束。如果获取失败,将线程封装为一个Node节点加入到等待队列中。
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
Node pred = tail;
if (pred != null) {//1、如果tail不为空,尝试快速插入队尾
node.prev = pred;
if (compareAndSetTail(pred, node)) {//2、CAS插入队尾成功,则退出
pred.next = node;
return node;
}
}
enq(node);//3、等待队列为空,或快速入队失败,则进行入队操作,直至成功
return node;
}
private Node enq(final Node node) {
for (;;) {
Node t = tail;
if (t == null) { //1、如果队列为空,初始化队列
if (compareAndSetHead(new Node()))//2、CAS设置头指向一个Node节点
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {//3、CAS设置node到队尾,直至成功
t.next = node;
return t;
}
}
}
}
将失败线程的Node节点加入到等待队列后,开始不断尝试获取锁
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();//1、获得node的前驱节点
if (p == head && tryAcquire(arg)) {//2、如果前驱节点已经是头节点,并且本线程获取锁成功
setHead(node);//3、设置自己为头节点
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&//4、获取锁失败,线程进入等待状态,设置waitStatus为-1
parkAndCheckInterrupt())
interrupted = true;//5、一旦线程被打断唤醒过,设置打断唤醒标记为true
}
} finally {
if (failed)
cancelAcquire(node);
}
}
private void setHead(Node node) {
head = node;
node.thread = null;
node.prev = null;
}
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)//1、如果waitStatus已经是-1了,即告诉前驱节点我需要你释放锁后通知我,则返回
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true;
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
//2、如果前驱节点waitStatus为1,即取消获取锁,则一直向前找,直到找到非取消状态的节点,将该节点和当前节点串联起来,
//中间的那些取消节点后续会被回收掉
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);//3、CAS设置waitStatus为-1
}
return false;
}
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);//1、在该锁上阻塞
return Thread.interrupted();//2、返回线程是否是被打断唤醒的,注意此方法会清除中断标记位
}
//LockSupport.java
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);//1、设置该线程被锁阻塞
UNSAFE.park(false, 0L);//2、线程阻塞,等待被唤醒
setBlocker(t, null);
}
至此,独占锁的获取过程已经结束。
独占释放
独占释放的顶层入口为release方法
public final boolean release(int arg) {
if (tryRelease(arg)) {//1、尝试释放锁
Node h = head;
if (h != null && h.waitStatus != 0)//2、等待队列不为空,且后续节点在等待头节点释放锁
unparkSuccessor(h);
return true;
}
return false;
}
//ReentrantLock覆写tryRelease
protected final boolean tryRelease(int releases) {
int c = getState() - releases;//1、获取当前还需退出锁的次数(因为重入锁可能进入不止一次)
if (Thread.currentThread() != getExclusiveOwnerThread())//2、当前线程才能释放自己持有的锁
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) {//3、如果当前需要退出锁的次数为0,说明线程已经不再持有锁,需释放锁
free = true;
setExclusiveOwnerThread(null);
}
setState(c);//4、设置state
return free;
}
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
* Thread to unpark is held in successor, which is normally
* just the next node. But if cancelled or apparently null,
* traverse backwards from tail to find the actual
* non-cancelled successor.
*/
Node s = node.next;
if (s == null || s.waitStatus > 0) {//1、找到后续节点waitStatus为-1的节点
s = null;
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);//2、唤醒等待锁释放的线程
}
在释放的最后,释放锁的线程唤醒在自己后面的下一个等待获取锁的线程。等待线程从park中醒来后,开始尝试获取锁。释放的过程到此结束。
共享获取
与独占模式不同,共享获取可以允许多个线程持有锁,所以共享状态state是可以大于1的。
public final void acquireShared(int arg) {
if (tryAcquireShared(arg) < 0)
doAcquireShared(arg);
}
protected int tryAcquireShared(int arg) {
throw new UnsupportedOperationException();
}
同样,tryAcquireShared没有具体实现,留给子类去实现自己的逻辑。共享模式以CountDownLatch举例。
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
//CountDownLatch用法
CountDownLatch countDownLatch = new CountDownLatch(3);
查看当前的state是否等于0。CountDownLatch在初始化时会将state设置为传进来的数值,上例中即state为3.
private void doAcquireShared(int arg) {
final Node node = addWaiter(Node.SHARED);//1、封装线程对象为Node,不过此时Node的模式为共享模式
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();//2、获得前驱节点
if (p == head) {//3、如果前驱节点已经是头节点
int r = tryAcquireShared(arg);//4、尝试获取共享锁
if (r >= 0) {//5、如果获取成功
setHeadAndPropagate(node, r);//6、将资源传播给下一个节点
p.next = null; // help GC
if (interrupted)
selfInterrupt();
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&//7、同独占模式
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
private void setHeadAndPropagate(Node node, int propagate) {
Node h = head; // Record old head for check below
setHead(node);//1、获取资源成功后,将自己设置为头节点
/*
* Try to signal next queued node if:
* Propagation was indicated by caller,
* or was recorded (as h.waitStatus either before
* or after setHead) by a previous operation
* (note: this uses sign-check of waitStatus because
* PROPAGATE status may transition to SIGNAL.)
* and
* The next node is waiting in shared mode,
* or we don't know, because it appears null
*
* The conservatism in both of these checks may cause
* unnecessary wake-ups, but only when there are multiple
* racing acquires/releases, so most need signals now or soon
* anyway.
*/
//2、如果资源还有剩余或者后续节点是共享节点
if (propagate > 0 || h == null || h.waitStatus < 0 ||
(h = head) == null || h.waitStatus < 0) {
Node s = node.next;
if (s == null || s.isShared())
doReleaseShared();//3、唤醒后继节点
}
}
共享释放
共享释放的顶层方法为releaseShared。
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
protected boolean tryReleaseShared(int arg) {
throw new UnsupportedOperationException();
}
看下tryReleaseShared在CountDownLatch中的实现。
protected boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
int c = getState();
if (c == 0)
return false;
int nextc = c-1;
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}
很好理解,当前线程释放一个共享资源,由于CountDownLatch是将资源总数减为0,所以这里释放后是将state减1,。如果是Semaphore释放资源后,state会进行加操作。
private void doReleaseShared() {
for (;;) {
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
if (ws == Node.SIGNAL) {
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h);
}
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}
源码注释比较清楚。unparkSuccessor和独占一样,唤醒等待节点。
condition
Condition需要和lock一起使用,在此以ReentrantLock举例。
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(()->{
lock.lock();
try {
log.info("{} 线程获取到锁", Thread.currentThread());
log.info("{} 线程调用condition.await", Thread.currentThread());
condition.await();
log.info("{} 线程被signal唤醒", Thread.currentThread());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
log.info("{} 线程释放锁", Thread.currentThread());
lock.unlock();
}
}).start();
new Thread(()->{
lock.lock();
try {
log.info("{} 线程获取到锁", Thread.currentThread());
log.info("{} 线程调用condition.signal", Thread.currentThread());
condition.signal();
log.info("{} 线程调用signal唤醒后", Thread.currentThread());
} finally {
log.info("{} 线程释放锁", Thread.currentThread());
lock.unlock();
}
}).start();
Condition是通过lock调用newCondition生成的。
public Condition newCondition() {
return sync.newCondition();
}
final ConditionObject newCondition() {
return new ConditionObject();
}
就是new一个ConditionObject对象。
public class ConditionObject implements Condition, java.io.Serializable {
/** First node of condition queue. */
private transient Node firstWaiter;
/** Last node of condition queue. */
private transient Node lastWaiter;
public final void signal() {
...
}
public final void await() throws InterruptedException {
...
}
}
ConditionObject位于Aqs内部,使用的时候通过newCondition生成,形成一个等待队列。所以只有一个同步队列,但是可以有多个等待队列。
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();//1、生成Condition节点
int savedState = fullyRelease(node);//2、释放资源,并返回之前持有资源的数量
int interruptMode = 0;
while (!isOnSyncQueue(node)) {//3、是否已经在同步队列中,在第一次进来时,节点在等待队列,会走到4;如果别的线程调用singal唤醒该线程后,节点会被加到同步队列中,跳出循环
LockSupport.park(this);//4、阻塞线程
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
//5、尝试获取wait前需要的资源,即2的返回值
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
private Node addConditionWaiter() {
Node t = lastWaiter;
// If lastWaiter is cancelled, clean out.
if (t != null && t.waitStatus != Node.CONDITION) {
unlinkCancelledWaiters();
t = lastWaiter;
}
Node node = new Node(Thread.currentThread(), Node.CONDITION);
if (t == null)
firstWaiter = node;
else
t.nextWaiter = node;
lastWaiter = node;
return node;
}
final int fullyRelease(Node node) {
boolean failed = true;
try {
int savedState = getState();//1、获取当前状态
if (release(savedState)) {//2、释放资源,唤醒后继节点
failed = false;//3、正常情况是可以成功的,因为线程此时已经拥有了锁
return savedState;
} else {
throw new IllegalMonitorStateException();
}
} finally {
if (failed)
node.waitStatus = Node.CANCELLED;
}
}
调用await,生成节点放进等待队列中,释放已占有的资源,等待唤醒。
public final void signal() {
if (!isHeldExclusively())//1、当前线程必须已经占有锁
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);//2、重置节点状态,将节点加入同步队列
}
private void doSignal(Node first) {
do {
if ( (firstWaiter = first.nextWaiter) == null)
lastWaiter = null;
first.nextWaiter = null;//节点移出等待队列
} while (!transferForSignal(first) &&
(first = firstWaiter) != null);
}
final boolean transferForSignal(Node node) {
/*
* If cannot change waitStatus, the node has been cancelled.
*/
//1、重置节点的状态为0
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;
/*
* Splice onto queue and try to set waitStatus of predecessor to
* indicate that thread is (probably) waiting. If cancelled or
* attempt to set waitStatus fails, wake up to resync (in which
* case the waitStatus can be transiently and harmlessly wrong).
*/
Node p = enq(node);//2、将节点加入同步队列中
int ws = p.waitStatus;
//3、如果状态为取消或者设置状态为SIGNAL失败,唤醒节点
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}
signal主要将节点状态由等待重置为0,并将节点从等待队列移除,加入同步队列。唤醒操作是从等待队列的首节点开始的。
一个示例图如下
synchronized、wait、notify对应于JUC中就是lock、await、signal,不同于wait、notify只能作用于同一个对象,一个lock可以生成多个Condition,对应于多个等待队列,使用上更加灵活。