AbstractQueuedSynchronizer.ConditionObject::enableWait源码笔记

// 函数作用:
// 1.设定node的各项参数
// 2.将node加入ConditionNode等待队列
// 3.释放当前线程持有的锁
private int enableWait(ConditionNode node) {
     if (isHeldExclusively()) {
          // node.waiter设置为当前线程
          // node的COND位和WAITING为同时被置位,也就是说node的status为0011
          node.waiter = Thread.currentThread();
          node.setStatusRelaxed(COND | WAITING);
          // node入队
          ConditionNode last = lastWaiter;
          if (last == null)
               firstWaiter = node;
          else
               last.nextWaiter = node;
          lastWaiter = node;
          // 1.获取当前锁的状态
          // 2.释放锁
          // 3.返回当前锁的状态,之所以要返回是因为之后被唤醒后还要使用此状态重新获取锁
          int savedState = getState();
          if (release(savedState))
               return savedState;
     }
     node.status = CANCELLED; // lock not held or inconsistent
     throw new IllegalMonitorStateException();
}

 

posted @ 2021-04-20 20:32  榆木脑袋0v0  阅读(75)  评论(0)    收藏  举报