0
点赞
收藏
分享

微信扫一扫

并发编程-Condition

上一篇 <<<AQS同步器
下一篇 >>>CountDownLatch同步计数器


Condition用法

Condition的队列

关键代码

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)) {
// 如果当前线程为-2 则当前线程变为阻塞状态
        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);
}
public final void signal() {
    if (!isHeldExclusively())
        throw new IllegalMonitorStateException();
//获取单向链表,
    Node first = firstWaiter;
    if (first != null)
        doSignal(first);
}

相关文章链接:
<<<多线程基础
<<<线程安全与解决方案
<<<锁的深入化
<<<锁的优化
<<<Java内存模型(JMM)
<<<Volatile解决JMM的可见性问题
<<<Volatile的伪共享和重排序
<<<CAS无锁模式及ABA问题
<<<Synchronized锁
<<<Lock锁
<<<AQS同步器
<<<CountDownLatch同步计数器
<<<Semaphore信号量
<<<CyclicBarrier屏障
<<<线程池
<<<并发队列
<<<Callable与Future模式
<<<Fork/Join框架
<<<Threadlocal
<<<Disruptor框架
<<<如何优化多线程总结

举报

相关推荐

0 条评论