虽然很少和AQS直接打交道,但是Java并发工具的底层基础,所以还是要了解队列同步器的运行机制
直接看源代码
AbstractQueuedSynchronizer 内部有两个内部类:Node和ConditionObject,是aqs的基石。
public abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer {
public class ConditionObject implements Condition {
private transient Node firstWaiter;
private transient Node lastWaiter;
}
static final class Node {
static final Node SHARED = new Node();
volatile int waitStatus;
volatile Node prev;
volatile Node next;
volatile Thread thread;
Node nextWaiter;
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;
}
}
}