AbstractQueuedSynchronizer 抽象同步队列简称 AQS,
它是实现同步器的 基 础组件, 并发包中锁的底层就是使用 AQS 实现的 。
AQS是一个FIFO的双向队列,其内部通过节点head和tail记录队 首和队尾元素,队列元素的类型为 Node。
其中 Node 中的 thread变量用来存放进入 AQS 队列里面的线程: Node 节点内部的 SHARED 用来标记该线程是获取共 享 资源时被阻 塞 挂起后放入 AQS 队列的, EXCLUSIVE 用来标记线程是 获 取独占资源时被挂起后放入 AQS 队列的 ;
waitStatus 记录当前线程等待状态:
CANCELLED (线程被取消了)、
SIGNAL (线程需要被唤醒)、
CONDITION (线程在条件队列里面等待〉、
PROPAGATE (释 放共享资源时需要通知其他节点〕;
prev 记录当前节点的前驱节点, next 记录当前节点的 后继节点 。
在独占方式下获取和释放资源使用的 方法为 :
void acquire(int arg)
void acquirelnterruptibly(int arg)
boolean release(int arg)。
在共享方式下获取和释放资源的方法为:
void acquireShared(int arg)
void acquireSharedinterruptibly(int arg)
boolean releaseShared(int arg)。
使用独占方式获取的资源是与具体线程绑定的,就是说如果 一 个 线程获取到了资源, 就会标记是这个线程获取到了,其他线程再尝试操作 state 获取资源时会发现当前该资源 不是自己持有的,就会在获取失败后被阻塞 。
队列中的实体 Node :
Node 这是一个双向队列
static final class Node {
//todo 模式,分为共享与独占
// todo 共享模式
/** Marker to indicate a node is waiting in shared mode */
static final Node SHARED = new Node();
// todo 独占模式
/** Marker to indicate a node is waiting in exclusive mode */
static final Node EXCLUSIVE = null;
// todo 结点状态
// todo CANCELLED,值为1,表示当前的线程被取消
// todo SIGNAL,值为-1,表示当前节点的后继节点包含的线程需要运行,也就是unpark
// todo CONDITION,值为-2,表示当前节点在等待 condition ,也就是在 condition 队列中
// todo PROPAGATE,值为-3,表示当前场景下后续的 acquireShared 能够得以执行
// todo 值为0,表示当前节点在sync队列中,等待着获取锁
/** 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).
*/
// todo 结点状态
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.
*/
// todo 前驱结点
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.
*/
// todo 后继结点
volatile Node next;
/**
* The thread that enqueued this node. Initialized on
* construction and nulled out after use.
*/
// todo 结点所对应的线程
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.
*/
// todo 下一个等待者
Node nextWaiter;
/**
* Returns true if node is waiting in shared mode.
*/
// todo 结点是否在共享模式下等待
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
*/
// todo 获取前驱结点,若前驱结点为空,抛出异常
final Node predecessor() throws NullPointerException {
// todo 保存前驱结点
Node p = prev;
// todo 前驱结点为空,抛出异常
if (p == null)
throw new NullPointerException();
else // todo 前驱结点不为空,返回
return p;
}
// todo 无参构造函数
Node() { // Used to establish initial head or SHARED marker
}
// todo 构造函数
Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
}
// todo 构造函数
Node(Thread thread, int waitStatus) { // Used by Condition
this.waitStatus = waitStatus;
this.thread = thread;
}
}
另外一个 队列 ConditionObject :
这是一个单向队列
// todo condition队列的头结点
/** First node of condition queue. */
private transient Node firstWaiter;
// todo condition队列的尾结点
/** Last node of condition queue. */
private transient Node lastWaiter;