ConditionObject
屬性 | 說明 |
Node firstWaiter | 頭節點 |
Node lastWaiter | 尾節點 |
為Condition接口實作,Condition的目的主要是替代Object的wait,notify,notifyAll方法的,它是基于Lock實作的.(而Lock是來替代synchronized方法).
結構

使用時序圖
關鍵方法
阻塞線程:await
對應Object.wait(),通過AQS機制釋放鎖定的資源,終止目前線程,恢複後使用AQS獨占模式重新鎖定資源
acquireQueued:此時node節點已轉換為AQS中節點
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
long 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);
}
喚醒線程:signal
transferForSignal轉換節點後await()中acquireQueued(node,savedState)操作的節點已是AQS中的節點
isHeldExclusively:子類實作.判斷是否獨家持有
public final void signal() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);
}