AQS阅读笔记
1 简介
AQS全称AbstractQueuedSynchronizer,是java几种锁或者同步工具(ReentrantLock、CountDownLatch、Semaphore、ThreadPoolExecutor.Worker的底层实现。其中用到了cas操作来修改值,cas是一个原子操作,确保同时只有一个线程可以修改成功,具体就不在这详细说了。
2 继承
父类AbstractOwnableSynchronizer。用来保存持有排他锁对象的线程,这样锁就可以实现重入机制,可重入锁(ReentrantLock)和可重入读写锁(ReentrantReadWriteLock)都用到了。
3 AQS属性、内部类
3.1 Node
Node是AQS内部类,等待锁的线程都被封装到node中,node通过prev、next属性可以组成线程等待链表。
3.1.1 waitStatus
0 Node创建时,默认为0
CANCELLED = 1; 表明当前节点取消获取锁。
SIGNAL = -1;。表明有一个后继节点需要唤醒。节点park之前会将前一节点waitstatus设为-1,唤醒后一节点前,也会将-1改为0。
CONDITION = -2;
PROPAGATE = -3;
3.2 stat
获取锁的数量。0表示当前没有线程使用锁。正常情况先获取锁,stat增加,然后释放所有锁之后,stat变回0。所以stat是不会<0的,如果<0表示stat超过了int范围。
4 获取排他锁
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
arg表示一次获取锁的个数。tryAcquire去尝试获取锁,但是aqs没有实现该方法,具体实现交给子类。下面是几个子类的实现。
4.1 tryAcquire尝试获取排它锁
4.1.1 ReentrantLock.NonfairSync
这个类是ReentrantLock的静态内部类,继承了Sync,Sync继承了aqs。
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
nonfairTryAcquire是父类Sync的方法
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
如果没有线程持有锁(stat==0),那么通过cas尝试获取锁,并通过setExclusiveOwnerThread方法设置排它锁持有线程为当前线程,cas成功返回true,否则返回false。
如果已经有线程持有锁,那么判断持有排它锁线程(getExclusiveOwnerThread)是否是当前线程,是则直接增加stat数量(由于持有锁是当前线程,所以不会有并发问题,不用cas修改)。如果nextc<0,表示持有锁数量超过int范围了,直接抛异常。
4.1.2 ReentrantLock.FairSync
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
和非公平锁区别是,尝试获取锁之前,多调用了hasQueuedPredecessors方法。判断等待队列中是否线程在等待,如果有,且等待的第一个Node是不是当前线程,则不会继续尝试获取锁。因为为了公平,先进入等待队列的线程需要先获取锁。
public final boolean hasQueuedPredecessors() {
// The correctness of this depends on head being initialized
// before tail and on head.next being accurate if the current
// thread is first in queue.
Node t = tail; // Read fields in reverse initialization order
Node h = head;
Node s;
return h != t &&
((s = h.next) == null || s.thread != Thread.currentThread());
}
如果队列不为空,且队里中等待的第一个node不是当前当前节点,则返回true。表示队列里有线程在自己之前
head是标记,不是实际等待的Node(具体可以看下面的enq方法),head.next是等待队列中第一个实际等待锁的Node。
(s = h.next) == null,这句是为了如果另一个线程刚好调用enq方法,刚设置完head和tail,此时还没有将自身加入tail,那么h.next则可能为null。
所以公平锁有一种情况是不公平的,即第一个线程来的时候发现锁被占用,然后调用addWaiter方法,enq时,锁被释放,恰好第二个线程来了,这时会第二个线程会直接获取锁,而不是加入等待队列,因为第二个线程认为此时没有等待队列。
4.2 acquireQueue与其他线程竞争获取排他锁
acquireQueued(addWaiter(Node.EXCLUSIVE), arg)
加入等待队列
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) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
Node之间通过prev和next属性组成等待队列。
创建一个waiter Node节点,如果等待队列不为空,尝试加入队尾。加入失败或者等待队列为空则调用enq方法。
private Node enq(final Node node) {
for (;;) {
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
循环通过cas加入队列。
如果队空,则尝试创建头节点(注意,头结点不是当前节点,而是new Node()),并设置队尾,然后进入下次循环。
如果队不为空,则prev指向队尾,然后cas设置队尾为当前节点。
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
如果前一节点是head,那么说明当前节点刚刚加入队列,那么此时很有可能锁已经被释放了,所以tryAcquire尝试获取锁,获取成功,则可以不用cas,并直接设置head为当前节点,因为其他线程即使尝试加入队列,也是加入队尾,而此时修改head是线程安全的。当然head依然是标记作用,并不是一个真正等待锁的Node,所以将当前节点设为head就表示当前节点已经获取到锁了。
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)
/*
* 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.
*/
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);
}
return false;
}
如果前一节点被设置成Node.SIGNA(允许唤醒后一节点),则返回true。
如果前一节点取消等待锁,则循环将所有前一节点为取消状态的节点删除。返回false;
否则将前一节点设为允许唤醒后一节点。返回false。
返回true时,调用parkAndCheckInterrupt()进入阻塞状态,返回false这是因为经过一些刚刚返回false的操作后,锁可能已经释放了,所以再尝试一次(尽可能不park,减少线程切换的损失)。
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
该方法将当前线程阻塞,并被唤醒后返回是否被interrupt。
5 释放排它锁
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
tryRelease由子类实现,尝试释放排它锁。如果如果释放成功,判断head是否允许唤醒后继节点,允许则通过unparkSuccessor唤醒后继节点。
5.1 尝试释放排它锁
下面介绍tryRelease的具体实现
5.1.1 ReentrantLock.Sync.tryRelease
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
这里先判断是否是当前线程释放锁,如果不是则抛异常。避免线程被我们手动unpark带来问题。
如果stat变成0,则释放锁成功,调用setExclusiveOwnerThread(null);,表明排它锁没有线程占用了。
5.2 唤醒后继节点
如果等待队列不为空,且head的waitStatus!=0(正常节点park之前会将前一节点waitStatus设为-1),则唤醒后继节点。
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) {
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);
}
如果当前节点可以唤醒后继节点(ws<0),则获取next。
如果next不为空且状态为取消(ws>0),则从后往前获取一个没有取消的节点。从后往前遍历是因为enq时,先设置tail,此时t.next = node;这行代码可能还未执行,如果从前往后,根据next拿不到tail,但实际上tail已经更新了。
最后如果next不为空,则unpart等待的线程,线程从acquireQueued的parkAndCheckInterrupt方法中唤醒。
6 取消获取锁
如果调用doAcquireNanos或者acquireInterruptibly获取锁,那么有可能获取锁失败,acquireQueued在异常情况下,也有可能获取锁失败。
那么当该线程被唤醒之后,就会触发finally代码块中的cancelAcquire方法。
private void cancelAcquire(Node node) {
// Ignore if node doesn't exist
if (node == null)
return;
node.thread = null;
// 删除被取消的前继节点
Node pred = node.prev;
while (pred.waitStatus > 0)
node.prev = pred = pred.prev;
// predNext is the apparent node to unsplice. CASes below will
// fail if not, in which case, we lost race vs another cancel
// or signal, so no further action is necessary.
Node predNext = pred.next;
// Can use unconditional write instead of CAS here.
// After this atomic step, other Nodes can skip past us.
// Before, we are free of interference from other threads.
node.waitStatus = Node.CANCELLED;
// If we are the tail, remove ourselves.
if (node == tail && compareAndSetTail(node, pred)) {
compareAndSetNext(pred, predNext, null);
} else {
// If successor needs signal, try to set pred's next-link
// so it will get one. Otherwise wake it up to propagate.
int ws;
if (pred != head &&
((ws = pred.waitStatus) == Node.SIGNAL ||
(ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) &&
pred.thread != null) {
Node next = node.next;
if (next != null && next.waitStatus <= 0)
compareAndSetNext(pred, predNext, next);
} else {
unparkSuccessor(node);
}
node.next = node; // help GC
}
}
因为head是标记作用,所以当前节点是tail时,可以直接删掉。
unparkSuccessor是因为如果该节点是等待队列首节点,显然他被cancel了就无法执行release方法中的unparkSuccessor方法了,所以需要唤醒下一个Node,即阻塞队列首节点。这会导致一个问题:prev.next没有更新。不过只要状态为cancel,那么release时就会跳过该节点,所以问题不大。
由于有cancel节点在正常节点之前,那么shouldParkAfterFailedAcquire方法会删除cancel的节点。或者被取消节点也会通过cancelAcquire删除cancel节点
7 Condition
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
final boolean transferForSignal(Node node) {
/*
* If cannot change waitStatus, the node has been cancelled.
*/
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);
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}
简单说就是如果调用await方法,就会释放锁,同时加入condition自己的队列,如果signal就将firstWaiter通过enq方法加入等待队列。如果此时的队尾节点是cancel状态或者将队尾节点设为signal状态失败,那么unparkSuccessor就无法唤醒当前节点,所以需要先将当前节点唤醒,然后进入acquireQueued,该方法中的shouldParkAfterFailedAcquire,确保调用part前,前一节点状态设为signal。