1.釋放鎖
2.輪詢檢查線程是否在同步線程上,如果在則退出自旋。否則檢查是否已超過解除掛起時(shí)間,如果超過,則退出掛起,否則繼續(xù)掛起線程到等待解除掛起。
3.退出掛起之后,采用自旋的方式競(jìng)爭(zhēng)鎖。
public final long awaitNanos(long nanosTimeout)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
//將
Node node = addConditionWaiter();
//
long savedState = fullyRelease(node);
final long deadline = System.nanoTime() + nanosTimeout;
int interruptMode = 0;
//采用自旋的方式檢查是否已在同步隊(duì)列當(dāng)中
while (!isOnSyncQueue(node)) {
//如果掛起超過一定的時(shí)間,則退出
if (nanosTimeout <= 0L) {
transferAfterCancelledWait(node);
break;
}
//繼續(xù)掛起線程
if (nanosTimeout >= spinForTimeoutThreshold)
LockSupport.parkNanos(this, nanosTimeout);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
nanosTimeout = deadline - System.nanoTime();
}
//采用自旋的方式競(jìng)爭(zhēng)鎖
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
return deadline - System.nanoTime();
}