Condition中的await()实现原理

await()源码

      public final void await() throws InterruptedException {
            //判断中断
            if (Thread.interrupted())
                throw new InterruptedException();
                //将当前线程封装成Node节点后入等待队列
            Node node = addConditionWaiter();
            //释放拿到的同步状态
            int savedState = fullyRelease(node);
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                //当线程不再同步队列中将其阻塞,置为wait状态
                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);
        }

你可能感兴趣的:(Java)