在CyclicBarrier里面就用到这个类
这个this$0还带上了外部类的引用
private final ReentrantLock lock = new ReentrantLock(); /** Condition to wait on until tripped */ private final Condition trip = lock.newCondition();
在CyclicBarrier是这样的那后面的fullyRelease可以理解了
ReentrantLock.newCondition创建出来的,两个属性
/** First node of condition queue. */ private transient Node firstWaiter; /** Last node of condition queue. */ private transient Node lastWaiter;
进入await()里面到这个类AbstractQueuedSynchronizer
public final void await() throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); //创建新节点 Node node = addConditionWaiter(); //把自己释放掉唤醒等待线程 int savedState = fullyRelease(node); int interruptMode = 0; //看看获取锁队列的情况,再看看自己的节点状态,判断是不是要wait 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); }
//新建个节点加到链表里面signalAll()就是唤醒这个链表的
private Node addConditionWaiter() { Node t = lastWaiter; // 尾部状态不是condition了 if (t != null && t.waitStatus != Node.CONDITION) { //这边从头开始清除掉状态不是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(); if (release(savedState)) { failed = false; return savedState; } else { throw new IllegalMonitorStateException(); } } finally { if (failed) node.waitStatus = Node.CANCELLED; } }