点击此处去Gitee上Clone源码下来在IDE上看效果更佳
package java.util.concurrent.locks;
import sun.misc.Unsafe;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* 提供一个框架,用于实现依赖于先进先出(FIFO)等待队列的阻塞锁和相关的同步器(信号灯,事件等)
* 此类旨在为大多数依赖单个原子{@code int}值表示状态的同步器提供有用的基础。
* 子类必须定义更改此状态的protected方法,并定义该状态对于获取或释放此对象而言意味着什么。
*/
public abstract class AbstractQueuedSynchronizer
extends AbstractOwnableSynchronizer
implements java.io.Serializable {
private static final long serialVersionUID = 7373984972572414691L;
protected AbstractQueuedSynchronizer() {
}
/**
* 等待队列节点类。
*/
static final class Node {
/**
* Node是共享模式
*/
static final Node SHARED = new Node();
/**
* Node是排他模式
*/
static final Node EXCLUSIVE = null;
volatile int waitStatus;
/**
* 线程已取消
* 说明:由于超时或中断,该节点被取消。节点永远不会离开此状态。
* 特别是,取消节点的线程永远不会再次阻塞。
*/
static final int CANCELLED = 1;
/**
* 后续线程需要释放
* 说明:该节点的后续节点在阻塞状态(或即将进入),
* 因此,当前节点释放或取消时必须unPark后继节点。
*/
static final int SIGNAL = -1;
/**
* 该节点当前在条件队列中。
*/
static final int CONDITION = -2;
/**
* releaseShared应该传播到其他节点。
* 在doReleaseShared中对此进行了设置(仅适用于头节点),
* 这个状态是为了修复一个BUG:JDK-6801020 : Concurrent Semaphore release may cause some require thread not signaled
* 这个状态是为了确保doReleaseShared能非常稳定地进行。
*/
static final int PROPAGATE = -3;
/**
* 当前节点的前节点,以检查waitStatus。
* 在入队期间分配,并且仅在出队时将其清空(出于GC的考虑)。
* 同样,在取消前任后,我们会短路,同时找到一个未取消的前任,
* 这将始终存在,因为根节点永远不会被取消:只有成功获取后,结点才变为根。
* 被取消的线程永远不会成功获取,并且一个线程只会取消自身,而不会取消任何其他节点。
*/
volatile Node prev;
/**
* 当前节点的下一个节点,当前节点/线程在释放时将其unpark。
* 在排队期间分配,在绕过CANCELLED的前节点时进行调整,并在出队时清零(出于GC的考虑)。
* enq操作直到附加后才分配前任的下一个字段,因此看到空的next不一定表示节点在队列末尾。
* 但是,如果下一个字段似乎为空,则我们可以从尾部扫描上一个以进行再次检查。
* CANCELLED节点的next字段设置为指向节点本身而不是null,以使isOnSyncQueue的工作更轻松。
*/
volatile Node next;
/**
* 使该节点入队的线程。
* 在构造上初始化,使用后消失。
*/
volatile Thread thread;
/**
* 等待队列中,nextWaiter表示当前Node是共享模式还是排他模式。
* 在条件队列中,nextWaiter才表示下一个节点
*/
Node nextWaiter;
/**
* 查看节点是否是共享节点
*/
final boolean isShared() {
return nextWaiter == SHARED;
}
/**
* 返回上一个节点
*/
final Node predecessor() throws NullPointerException {
Node p = prev;
if (p == null)
throw new NullPointerException();
else
return p;
}
Node() { // 用于建立初始标头或SHARED标记
}
Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
}
Node(Thread thread, int waitStatus) { // 在条件队列中使用
this.waitStatus = waitStatus;
this.thread = thread;
}
}
/**
* 等待队列的头,延迟初始化。
* 除初始化外,只能通过setHead方法进行修改。
* 注意:如果head存在,则保证其waitStatus不能是CANCELLED。
* 头节点在第一次入队后永远存在。
* 头节点不存储线程。
*/
private transient volatile Node head;
/**
* 等待队列的尾部,延迟初始化。
* 仅通过方法enq进行修改以添加新的等待节点。
*/
private transient volatile Node tail;
/**
* 同步状态。
*/
private volatile int state;
/**
* @return 当前状态值
*/
protected final int getState() {
return state;
}
/**
* 设置同步状态的值。
*
* @param newState 新状态值
*/
protected final void setState(int newState) {
state = newState;
}
/**
* 如果当前状态值等于期望值,则以原子方式将同步状态设置为给定的更新值。
* 此操作具有{@code volatile}读写的内存语义。
*
* @param expect 期望值
* @param update 新值
* @return {@code true} if successful.
*/
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
// Queuing utilities
/**
* The number of nanoseconds for which it is faster to spin
* rather than to use timed park. A rough estimate suffices
* to improve responsiveness with very short timeouts.
*/
static final long spinForTimeoutThreshold = 1000L;
/**
* 入队
*
* @param node the node to insert
* @return node's 前节点
*/
private Node enq(final Node node) {
for (; ; ) {
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))//设置头节点和尾节点
tail = head;
} else {//把node添加到尾节点
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
/**
* 为当前线程和给定模式创建并入队节点。
*
* @param mode {@link Node#EXCLUSIVE}用于独占,{@link Node#SHARED}用于共享
* @return 新节点
*/
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// 简单并快速地尝试enq;失败再调用enq方法。
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
/**
* 将队列头设置为节点,从而出队。
* 仅通过acquire方法调用。
* 为了GC并抑制不必要的信号和遍历,还可以清空未使用的字段。
*
* @param node the node
*/
private void setHead(Node node) {
head = node;
node.thread = null;//头节点不存储线程
node.prev = null;
}
// Utilities for various versions of acquire
/**
* 一种方便的方法来中断当前线程。
*/
static void selfInterrupt() {
Thread.currentThread().interrupt();
}
/**
* park的便捷方法,然后检查是否中断
*
* @return {@code true} 如果被打断
*/
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
/**
* 判断当前节点能否阻塞,把前置节点设置为SIGNAL才能park
*
* @param pred 前置节点
* @param node the node
* @return {@code true} 如果线程应该阻塞
*/
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;//前置节点的状态
if (ws == Node.SIGNAL)
/*
前置节点已经是SIGNAL状态了,返回true
*/
return true;
if (ws > 0) {//前置节点是取消状态,需要找到一个非取消节点作为前置节点
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
/*
把前置节点状态设置为SIGNAL
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
// 需要子类实现的方法
protected boolean tryAcquire(int arg) {
throw new UnsupportedOperationException();
}
protected boolean tryRelease(int arg) {
throw new UnsupportedOperationException();
}
protected int tryAcquireShared(int arg) {
throw new UnsupportedOperationException();
}
/**
*
*/
protected boolean tryReleaseShared(int arg) {
throw new UnsupportedOperationException();
}
/**
* 检查当前线程是否是独占锁
*
* @return {@code true} if synchronization is held exclusively;
* {@code false} otherwise
* @throws UnsupportedOperationException if conditions are not supported
*/
protected boolean isHeldExclusively() {
throw new UnsupportedOperationException();
}
/*排他锁相关方法*/
/**
* 在独占模式下获取锁,不会响应中断,只会把线程设置为中断状态。
* 通过至少调用一次{@link #tryAcquire}并成功返回来实现。
* 否则,线程将排队,并可能反复阻塞和解除阻塞,并调用{@link #tryAcquire}直到成功。
* 此方法可用于实现方法{@link Lock#lock}。
* <p>
* 注意:此方法被中断后不会抛出异常,会继续往下执行,可以检查线程的中断状态来做出相应处理
*
* @param arg the acquire 参数.
* 此值传送到{@link #tryAcquire}
* 没有固定使用,可以代表您喜欢的任何内容。
*/
public final void acquire(int arg) {
//先尝试一下能不能抢到锁,抢不到再去排队。
if (!tryAcquire(arg) &&//子类实现的方法,一般都是cas给state赋值来决定是否能获得锁
//如果没有成功获取锁,则进入等待队列,然后再调用acquireQueued方法
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();//因为在acquireQueued中判断线程中断状态是用的Thread.interrupt,所以还需重新设置为中断状态
}
/**
* 以排他的无法响应中断的模式尝试获取锁。
*
* @param node the node
* @param arg the acquire argument
* @return {@code true} 如果在等待时被打断
*/
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())//park,并且unpark后会检查是否是因为中断而unpark的
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/**
* 以排他的可以响应中断的模式尝试获取锁。
*
* @throws InterruptedException 如果当前线程被中断
*/
public final void acquireInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (!tryAcquire(arg))
doAcquireInterruptibly(arg);
}
/**
* 以排他的响应中断的模式尝试获取锁。
*
* @param arg the acquire argument
*/
private void doAcquireInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.EXCLUSIVE);
boolean failed = true;
try {
for (; ; ) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
//如果发生了中断会抛出InterruptedException
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/**
* 以排他的响应中断的模式尝试获取锁。有超时时间
*
* @param nanosTimeout 等待的最大纳秒数
* @return {@code true} if acquired; {@code false} if timed out
* @throws InterruptedException if the current thread is interrupted
*/
public final boolean tryAcquireNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
return tryAcquire(arg) ||
doAcquireNanos(arg, nanosTimeout);
}
/**
* 以排他定时模式进行获取。响应中断。
*
* @param arg the acquire argument
* @param nanosTimeout 最大等待时间
* @return {@code true} if acquired
*/
private boolean doAcquireNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (nanosTimeout <= 0L)
return false;
final long deadline = System.nanoTime() + nanosTimeout;
final Node node = addWaiter(Node.EXCLUSIVE);
boolean failed = true;
try {
for (; ; ) {
final Node p = node.predecessor();
//尝试获取锁
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return true;
}
nanosTimeout = deadline - System.nanoTime();
if (nanosTimeout <= 0L)
return false;
if (shouldParkAfterFailedAcquire(p, node) &&
nanosTimeout > spinForTimeoutThreshold)
//最多阻塞一定时间
LockSupport.parkNanos(this, nanosTimeout);
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/*共享模式获取*/
/**
* 以共享模式获取,忽略中断。
* 通过首先至少调用一次{@link #tryAcquireShared}并成功返回来实现。
* 否则,线程将排队,并可能反复阻塞和解除阻塞,并调用{@link #tryAcquireShared}直到成功。
*/
public final void acquireShared(int arg) {
//先尝试一下能不能抢到锁,抢不到再去排队。
if (tryAcquireShared(arg) < 0)
doAcquireShared(arg);
}
/**
* 以共享的模式进行获取。不响应中断
*
* @param arg the acquire argument
*/
private void doAcquireShared(int arg) {
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
boolean interrupted = false;
for (; ; ) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {//大于0说明成功获取了锁
//把当前节点设置为头节点并且在后继节点不是排他模式的情况下释放所有节点
setHeadAndPropagate(node, r);
p.next = null; // help GC
if (interrupted)
selfInterrupt();
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;//发生了中断
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/**
* 以共享的模式进行获取。响应中断
*/
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)//先简单地尝试获取锁
doAcquireSharedInterruptibly(arg);
}
private void doAcquireSharedInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.SHARED);//先进入同步队列
boolean failed = true;
try {
for (; ; ) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();//抛出中断异常
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/**
* 以共享的模式进行获取。响应中断,有超时时间
*/
public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
return tryAcquireShared(arg) >= 0 ||
doAcquireSharedNanos(arg, nanosTimeout);
}
private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (nanosTimeout <= 0L)
return false;
final long deadline = System.nanoTime() + nanosTimeout;
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
for (; ; ) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return true;
}
}
nanosTimeout = deadline - System.nanoTime();
if (nanosTimeout <= 0L)
return false;
if (shouldParkAfterFailedAcquire(p, node) &&
nanosTimeout > spinForTimeoutThreshold)
//park一定的时间
LockSupport.parkNanos(this, nanosTimeout);
if (Thread.interrupted())
//响应中断
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
/**
* Cancels an ongoing attempt to acquire.
*
* @param node the node
*/
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;
//获取到前节点的next
Node predNext = pred.next;
//把要取消的节点状态设置为CANCELLED
node.waitStatus = Node.CANCELLED;
//如果当前节点是tail,把前一个节点设置为tail
if (node == tail && compareAndSetTail(node, pred)) {
compareAndSetNext(pred, predNext, null);
} else {
// 如果后继者需要信号,请尝试设置pred的next为后继者。
// 否则唤醒它以传播。
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
}
}
/**
* 1:把当前节点设置成头节点
* 2:看看后续节点有无正在等待,并且也是共享模式的,有的话释放这些节点
*
* @param node the node
* @param propagate the return value from a tryAcquireShared
*/
private void setHeadAndPropagate(Node node, int propagate) {
Node h = head; // Record old head for check below
setHead(node);
// propagate > 0 表示已经有节点获得了共享锁,
// 或者waitStatus为SIGNAL或PROPAGATE,都应该去尝试释放共享节点
if (propagate > 0 || h == null || h.waitStatus < 0 ||
(h = head) == null || h.waitStatus < 0) {
Node s = node.next;
//为了防止写锁饥饿,后继节点不是排他模式的节点,才能唤醒所有的节点。
if (s == null || s.isShared())
doReleaseShared();
}
}
/**
* 唤醒节点的后继者(如果存在)。
*
* @param node the node
*/
private void unparkSuccessor(Node node) {
/*
将节点状态设置为初始状态
*/
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
unpark保存在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);
}
/**
* 独占模式锁释放
*/
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
//头节点不为空并且不是初始化状态
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);//唤醒头节点的后继节点
return true;
}
return false;
}
/*共享模式释放*/
/**
* 共享锁释放
*/
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
/**
* 唤醒所有前置节点状态为SIGNAL的节点。
*/
private void doReleaseShared() {
for (; ; ) {
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
if (ws == Node.SIGNAL) {
//头节点状态为SIGNAL说明下一个节点需要唤醒,设置状态成功就可以进行unparkSuccessor
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h);
} else if (ws == 0 &&
//将头节点状态设置为PROPAGATE,为了非常稳重地doReleaseShared
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
//头节点发生了变化,则说明有新的共享节点获取到了锁,继续循环
//当头节点不变时。说明所有共享锁的节点已经释放完成或者是释放了一个排他锁节点,则退出循环
if (h == head) // loop if head changed
break;
}
}
/*条件队列*/
/**
* {@link AbstractQueuedSynchronizer}的{@link Condition}实现,用作{@link Lock}实现的基础。
*
* <p>
* Method documentation for this class describes mechanics,
* not behavioral specifications from the point of view of Lock
* and Condition users. Exported versions of this class will in
* general need to be accompanied by documentation describing
* condition semantics that rely on those of the associated
* {@code AbstractQueuedSynchronizer}.
*
* <p>This class is Serializable, but all fields are transient,
* so deserialized conditions have no waiters.
*/
public class ConditionObject implements Condition, java.io.Serializable {
private static final long serialVersionUID = 1173984872572414699L;
/**
* 条件队列的第一个节点。
*/
private transient Node firstWaiter;
/**
* 条件队列的最后一个节点。
*/
private transient Node lastWaiter;
public ConditionObject() {
}
// Internal methods
/*等待方法*/
/**
* 进入条件队列等待
*/
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)) {
//如果此节点没有在同步队列中,则park
LockSupport.park(this);
//检测中断,如果出现了中断就break
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);
}
/**
* 带有超时时间的await
*/
public final long awaitNanos(long nanosTimeout)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
final long deadline = System.nanoTime() + nanosTimeout;
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
if (nanosTimeout <= 0L) {
//时间为负直接转移去同步队列
transferAfterCancelledWait(node);
break;
}
if (nanosTimeout >= spinForTimeoutThreshold)
//park一定时间
LockSupport.parkNanos(this, nanosTimeout);
//检测中断
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
nanosTimeout = deadline - System.nanoTime();
}
//以保存状态入队
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
//响应中断
reportInterruptAfterWait(interruptMode);
return deadline - System.nanoTime();
}
/**
* 和awaitNanos差不多
*/
public final boolean await(long time, TimeUnit unit)
throws InterruptedException {
long nanosTimeout = unit.toNanos(time);
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
final long deadline = System.nanoTime() + nanosTimeout;
boolean timedout = false;
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
if (nanosTimeout <= 0L) {
timedout = transferAfterCancelledWait(node);
break;
}
if (nanosTimeout >= spinForTimeoutThreshold)
LockSupport.parkNanos(this, nanosTimeout);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
nanosTimeout = deadline - System.nanoTime();
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
return !timedout;
}
/**
* 添加新的等待者进入条件队列。
*
* @return its new wait node
*/
private Node addConditionWaiter() {
Node t = lastWaiter;
// 如果lastWaiter被取消,请清除。
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;
}
/*唤醒相关方法*/
/**
* 将等待时间最长的线程(如果存在)从该条件的等待队列移至等待队列。
*
* @throws IllegalMonitorStateException if {@link #isHeldExclusively}
* returns {@code false}
*/
public final void signal() {
//如果当前锁不是独占的
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);
}
/**
* Moves all threads from the wait queue for this condition to
* the wait queue for the owning lock.
*
* @throws IllegalMonitorStateException if {@link #isHeldExclusively}
* returns {@code false}
*/
public final void signalAll() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignalAll(first);
}
private void doSignal(Node first) {
do {
if ((firstWaiter = first.nextWaiter) == null)
lastWaiter = null;
first.nextWaiter = null;
//直到成功转移成功一个节点或者队列为空为止
} while (!transferForSignal(first) &&
(first = firstWaiter) != null);
}
/**
* 删除并转移所有节点。
*
* @param first (non-null) the first node on condition queue
*/
private void doSignalAll(Node first) {
lastWaiter = firstWaiter = null;
do {
Node next = first.nextWaiter;
first.nextWaiter = null;
transferForSignal(first);
first = next;
} while (first != null);
}
/**
* 从条件队列中取消Cancelled的等待节点的链接。仅在保持锁定状态下调用。
* 在条件等待期间发生取消操作时,以及在看到lastWaiter已被取消时插入新的服务程序时调用此方法。
* 需要这种方法来避免在没有信号的情况下保留垃圾。
* 因此,即使可能需要完全遍历,它也只有在没有信号的情况下发生超时或取消时才起作用。
* 它遍历所有节点,而不是停在特定目标上,
* 以取消所有指向垃圾节点的指针的链接,而无需在取消风暴期间进行多次遍历。
*/
private void unlinkCancelledWaiters() {
Node t = firstWaiter;
Node trail = null;
//遍历unlink所有取消的节点
while (t != null) {
Node next = t.nextWaiter;
if (t.waitStatus != Node.CONDITION) {
t.nextWaiter = null;
if (trail == null)
firstWaiter = next;
else
trail.nextWaiter = next;
if (next == null)
lastWaiter = trail;
} else
trail = t;
t = next;
}
}
// public methods
/**
* Implements uninterruptible condition wait.
* <ol>
* <li> Save lock state returned by {@link #getState}.
* <li> Invoke {@link #release} with saved state as argument,
* throwing IllegalMonitorStateException if it fails.
* <li> Block until signalled.
* <li> Reacquire by invoking specialized version of
* {@link #acquire} with saved state as argument.
* </ol>
*/
public final void awaitUninterruptibly() {
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
boolean interrupted = false;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if (Thread.interrupted())
interrupted = true;
}
if (acquireQueued(node, savedState) || interrupted)
selfInterrupt();
}
/*
* For interruptible waits, we need to track whether to throw
* InterruptedException, if interrupted while blocked on
* condition, versus reinterrupt current thread, if
* interrupted while blocked waiting to re-acquire.
*/
/**
* Mode meaning to reinterrupt on exit from wait
*/
private static final int REINTERRUPT = 1;
/**
* Mode meaning to throw InterruptedException on exit from wait
*/
private static final int THROW_IE = -1;
/**
* 检查是否有中断,如果在signal之前被中断,则返回THROW_IE;
* 在signal之后,则返回REINTERRUPT;否则,则返回0。
*/
private int checkInterruptWhileWaiting(Node node) {
return Thread.interrupted() ?
(transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
0;
}
/**
* 根据模式,引发InterruptedException,重新中断当前线程或不执行任何操作。
*/
private void reportInterruptAfterWait(int interruptMode)
throws InterruptedException {
if (interruptMode == THROW_IE)
throw new InterruptedException();
else if (interruptMode == REINTERRUPT)
selfInterrupt();
}
/**
* Implements absolute timed condition wait.
* <ol>
* <li> If current thread is interrupted, throw InterruptedException.
* <li> Save lock state returned by {@link #getState}.
* <li> Invoke {@link #release} with saved state as argument,
* throwing IllegalMonitorStateException if it fails.
* <li> Block until signalled, interrupted, or timed out.
* <li> Reacquire by invoking specialized version of
* {@link #acquire} with saved state as argument.
* <li> If interrupted while blocked in step 4, throw InterruptedException.
* <li> If timed out while blocked in step 4, return false, else true.
* </ol>
*/
public final boolean awaitUntil(Date deadline)
throws InterruptedException {
long abstime = deadline.getTime();
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
boolean timedout = false;
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
if (System.currentTimeMillis() > abstime) {
timedout = transferAfterCancelledWait(node);
break;
}
LockSupport.parkUntil(this, abstime);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
return !timedout;
}
// support for instrumentation
/**
* Returns true if this condition was created by the given
* synchronization object.
*
* @return {@code true} if owned
*/
final boolean isOwnedBy(AbstractQueuedSynchronizer sync) {
return sync == AbstractQueuedSynchronizer.this;
}
/**
* Queries whether any threads are waiting on this condition.
* Implements {@link AbstractQueuedSynchronizer#hasWaiters(ConditionObject)}.
*
* @return {@code true} if there are any waiting threads
* @throws IllegalMonitorStateException if {@link #isHeldExclusively}
* returns {@code false}
*/
protected final boolean hasWaiters() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
if (w.waitStatus == Node.CONDITION)
return true;
}
return false;
}
/**
* Returns an estimate of the number of threads waiting on
* this condition.
* Implements {@link AbstractQueuedSynchronizer#getWaitQueueLength(ConditionObject)}.
*
* @return the estimated number of waiting threads
* @throws IllegalMonitorStateException if {@link #isHeldExclusively}
* returns {@code false}
*/
protected final int getWaitQueueLength() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
int n = 0;
for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
if (w.waitStatus == Node.CONDITION)
++n;
}
return n;
}
/**
* Returns a collection containing those threads that may be
* waiting on this Condition.
* Implements {@link AbstractQueuedSynchronizer#getWaitingThreads(ConditionObject)}.
*
* @return the collection of threads
* @throws IllegalMonitorStateException if {@link #isHeldExclusively}
* returns {@code false}
*/
protected final Collection<Thread> getWaitingThreads() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
ArrayList<Thread> list = new ArrayList<Thread>();
for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
if (w.waitStatus == Node.CONDITION) {
Thread t = w.thread;
if (t != null)
list.add(t);
}
}
return list;
}
}
// 队列检查方法
/**
* 查询是否有任何线程正在等待获取。
* 请注意,由于中断和超时导致的取消可能随时发生,因此{@code true}返回值不能保证任何其他线程都可以获取。
*
* @return {@code true},如果还有其他线程在等待获取
*/
public final boolean hasQueuedThreads() {
return head != tail;
}
/**
* @return {@code true} 如果曾经有线程进入同步队列
*/
public final boolean hasContended() {
return head != null;
}
/**
* Returns the first (longest-waiting) thread in the queue, or
* {@code null} if no threads are currently queued.
*
* <p>In this implementation, this operation normally returns in
* constant time, but may iterate upon contention if other threads are
* concurrently modifying the queue.
*
* @return the first (longest-waiting) thread in the queue, or
* {@code null} if no threads are currently queued
*/
public final Thread getFirstQueuedThread() {
// handle only fast path, else relay
return (head == tail) ? null : fullGetFirstQueuedThread();
}
/**
* Version of getFirstQueuedThread called when fastpath fails
*/
private Thread fullGetFirstQueuedThread() {
/*
* The first node is normally head.next. Try to get its
* thread field, ensuring consistent reads: If thread
* field is nulled out or s.prev is no longer head, then
* some other thread(s) concurrently performed setHead in
* between some of our reads. We try this twice before
* resorting to traversal.
*/
Node h, s;
Thread st;
if (((h = head) != null && (s = h.next) != null &&
s.prev == head && (st = s.thread) != null) ||
((h = head) != null && (s = h.next) != null &&
s.prev == head && (st = s.thread) != null))
return st;
/*
* Head's next field might not have been set yet, or may have
* been unset after setHead. So we must check to see if tail
* is actually first node. If not, we continue on, safely
* traversing from tail back to head to find first,
* guaranteeing termination.
*/
Node t = tail;
Thread firstThread = null;
while (t != null && t != head) {
Thread tt = t.thread;
if (tt != null)
firstThread = tt;
t = t.prev;
}
return firstThread;
}
/**
* Returns true if the given thread is currently queued.
*
* <p>This implementation traverses the queue to determine
* presence of the given thread.
*
* @param thread the thread
* @return {@code true} if the given thread is on the queue
* @throws NullPointerException if the thread is null
*/
public final boolean isQueued(Thread thread) {
if (thread == null)
throw new NullPointerException();
for (Node p = tail; p != null; p = p.prev)
if (p.thread == thread)
return true;
return false;
}
/**
* Returns {@code true} if the apparent first queued thread, if one
* exists, is waiting in exclusive mode. If this method returns
* {@code true}, and the current thread is attempting to acquire in
* shared mode (that is, this method is invoked from {@link
* #tryAcquireShared}) then it is guaranteed that the current thread
* is not the first queued thread. Used only as a heuristic in
* ReentrantReadWriteLock.
*/
final boolean apparentlyFirstQueuedIsExclusive() {
Node h, s;
return (h = head) != null &&
(s = h.next) != null &&
!s.isShared() &&
s.thread != null;
}
/**
* 查询是否有有排队的前辈。
*
* @return {@code true}如果当前线程之前有一个排队的线程,
* and {@code false}如果当前线程位于队列的开头或队列为空
* @since 1.7
*/
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());
}
// Instrumentation and monitoring methods
/**
* 返回等待获取的线程数的估计值。该值只是一个估计值,因为在此方法遍历内部数据结构时,线程数可能会动态变化。
* 此方法设计用于监视系统状态,而不用于同步控制。
*
* @return the estimated number of threads waiting to acquire
*/
public final int getQueueLength() {
int n = 0;
for (Node p = tail; p != null; p = p.prev) {
if (p.thread != null)
++n;
}
return n;
}
/**
* @return the collection of threads
*/
public final Collection<Thread> getQueuedThreads() {
ArrayList<Thread> list = new ArrayList<Thread>();
for (Node p = tail; p != null; p = p.prev) {
Thread t = p.thread;
if (t != null)
list.add(t);
}
return list;
}
/**
* @return the collection of 排他模式的 threads
*/
public final Collection<Thread> getExclusiveQueuedThreads() {
ArrayList<Thread> list = new ArrayList<Thread>();
for (Node p = tail; p != null; p = p.prev) {
if (!p.isShared()) {
Thread t = p.thread;
if (t != null)
list.add(t);
}
}
return list;
}
/**
* @return the collection of 共享模式的 threads
*/
public final Collection<Thread> getSharedQueuedThreads() {
ArrayList<Thread> list = new ArrayList<Thread>();
for (Node p = tail; p != null; p = p.prev) {
if (p.isShared()) {
Thread t = p.thread;
if (t != null)
list.add(t);
}
}
return list;
}
/**
* Returns a string identifying this synchronizer, as well as its state.
* The state, in brackets, includes the String {@code "State ="}
* followed by the current value of {@link #getState}, and either
* {@code "nonempty"} or {@code "empty"} depending on whether the
* queue is empty.
*
* @return a string identifying this synchronizer, as well as its state
*/
public String toString() {
int s = getState();
String q = hasQueuedThreads() ? "non" : "";
return super.toString() +
"[State = " + s + ", " + q + "empty queue]";
}
// Internal support methods for Conditions
/**
* 如果一个节点(总是一个最初放置在条件队列中的节点)现在正在等待在同步队列上重新获取,则返回true。
*
* @param node the node
* @return如果正在获取,则为true
*/
final boolean isOnSyncQueue(Node node) {
if (node.waitStatus == Node.CONDITION || node.prev == null)
return false;
if (node.next != null) // If has successor, it must be on queue
return true;
/*
* node.prev can be non-null, but not yet on queue because
* the CAS to place it on queue can fail. So we have to
* traverse from tail to make sure it actually made it. It
* will always be near the tail in calls to this method, and
* unless the CAS failed (which is unlikely), it will be
* there, so we hardly ever traverse much.
*/
return findNodeFromTail(node);
}
/**
* Returns true if node is on sync queue by searching backwards from tail.
* Called only when needed by isOnSyncQueue.
*
* @return true if present
*/
private boolean findNodeFromTail(Node node) {
Node t = tail;
for (; ; ) {
if (t == node)
return true;
if (t == null)
return false;
t = t.prev;
}
}
/**
* 将节点从条件队列转移到同步队列。如果成功,则返回true。
*
* @param node the node
* @return true if successfully transferred (else the node was
* cancelled before signal)
*/
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;
}
/**
* 取消节点后,如有必要,转移节点以同步队列。如果线程在signal之前被取消,则返回true。
*
* @param node the node
* @return true if cancelled before the node was signalled
*/
final boolean transferAfterCancelledWait(Node node) {
if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
enq(node);
return true;
}
/*
如果signal抢先了,就需要等待signal执行完enq方法
*/
while (!isOnSyncQueue(node))
Thread.yield();
return false;
}
/**
* 用当前状态值调用释放;返回保存状态。取消节点并在失败时引发异常。
*
* @param node the condition node for this wait
* @return 先前的同步状态
*/
final int fullyRelease(Node node) {
boolean failed = true;
try {
int savedState = getState();
if (release(savedState)) {
failed = false;
return savedState;
} else {
throw new IllegalMonitorStateException();
}
} finally {
if (failed)
node.waitStatus = Node.CANCELLED;
}
}
// Instrumentation methods for conditions
/**
* Queries whether the given ConditionObject
* uses this synchronizer as its lock.
*
* @param condition the condition
* @return {@code true} if owned
* @throws NullPointerException if the condition is null
*/
public final boolean owns(ConditionObject condition) {
return condition.isOwnedBy(this);
}
/**
* Queries whether any threads are waiting on the given condition
* associated with this synchronizer. Note that because timeouts
* and interrupts may occur at any time, a {@code true} return
* does not guarantee that a future {@code signal} will awaken
* any threads. This method is designed primarily for use in
* monitoring of the system state.
*
* @param condition the condition
* @return {@code true} if there are any waiting threads
* @throws IllegalMonitorStateException if exclusive synchronization
* is not held
* @throws IllegalArgumentException if the given condition is
* not associated with this synchronizer
* @throws NullPointerException if the condition is null
*/
public final boolean hasWaiters(ConditionObject condition) {
if (!owns(condition))
throw new IllegalArgumentException("Not owner");
return condition.hasWaiters();
}
/**
* Returns an estimate of the number of threads waiting on the
* given condition associated with this synchronizer. Note that
* because timeouts and interrupts may occur at any time, the
* estimate serves only as an upper bound on the actual number of
* waiters. This method is designed for use in monitoring of the
* system state, not for synchronization control.
*
* @param condition the condition
* @return the estimated number of waiting threads
* @throws IllegalMonitorStateException if exclusive synchronization
* is not held
* @throws IllegalArgumentException if the given condition is
* not associated with this synchronizer
* @throws NullPointerException if the condition is null
*/
public final int getWaitQueueLength(ConditionObject condition) {
if (!owns(condition))
throw new IllegalArgumentException("Not owner");
return condition.getWaitQueueLength();
}
/**
* Returns a collection containing those threads that may be
* waiting on the given condition associated with this
* synchronizer. Because the actual set of threads may change
* dynamically while constructing this result, the returned
* collection is only a best-effort estimate. The elements of the
* returned collection are in no particular order.
*
* @param condition the condition
* @return the collection of threads
* @throws IllegalMonitorStateException if exclusive synchronization
* is not held
* @throws IllegalArgumentException if the given condition is
* not associated with this synchronizer
* @throws NullPointerException if the condition is null
*/
public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
if (!owns(condition))
throw new IllegalArgumentException("Not owner");
return condition.getWaitingThreads();
}
/**
* Setup to support compareAndSet. We need to natively implement
* this here: For the sake of permitting future enhancements, we
* cannot explicitly subclass AtomicInteger, which would be
* efficient and useful otherwise. So, as the lesser of evils, we
* natively implement using hotspot intrinsics API. And while we
* are at it, we do the same for other CASable fields (which could
* otherwise be done with atomic field updaters).
*/
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long stateOffset;
private static final long headOffset;
private static final long tailOffset;
private static final long waitStatusOffset;
private static final long nextOffset;
static {
try {
stateOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("state"));
headOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("head"));
tailOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
waitStatusOffset = unsafe.objectFieldOffset
(Node.class.getDeclaredField("waitStatus"));
nextOffset = unsafe.objectFieldOffset
(Node.class.getDeclaredField("next"));
} catch (Exception ex) {
throw new Error(ex);
}
}
/**
* CAS头字段。仅由enq使用。
*/
private final boolean compareAndSetHead(Node update) {
return unsafe.compareAndSwapObject(this, headOffset, null, update);
}
/**
* CAS tail field. Used only by enq.
*/
private final boolean compareAndSetTail(Node expect, Node update) {
return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
}
/**
* CAS waitStatus field of a node.
*/
private static final boolean compareAndSetWaitStatus(Node node,
int expect,
int update) {
return unsafe.compareAndSwapInt(node, waitStatusOffset,
expect, update);
}
/**
* CAS next field of a node.
*/
private static final boolean compareAndSetNext(Node node,
Node expect,
Node update) {
return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
}
}