AbstractQueuedSynchronizer的ConditionObject类

在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;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值