AbstractQueuedSynchronizer.ConditionNode::unlinkCancelledWaiters源码笔记

/**
* Unlinks the given node and other non-waiting nodes from
* condition queue unless already unlinked.
*/
private void unlinkCancelledWaiters(ConditionNode node) {
     // if里的三个条件可以判断node的合法性
     // 1.node为空
     // 2.node不为尾结点时要求node.nextWaiter != null
     // 3.node为尾结点时要求node == lastWaiter
     if (node == null || node.nextWaiter != null || node == lastWaiter) {
          // w为当前节点,从首结点开始遍历
          // trail是前导节点指针
          ConditionNode w = firstWaiter, trail = null;
          while (w != null) {
               ConditionNode next = w.nextWaiter;
               // 判断成功说明w当前已经不处于等待状态,应该移除
               if ((w.status & COND) == 0) {
                    w.nextWaiter = null;
                    // 前导节点指针为null说明w当前是首节点,此时应该改变首节点指针
                    // 否则改变前导节点的nextWaiter属性
                    if (trail == null)
                         firstWaiter = next;
                    else
                         trail.nextWaiter = next;
                    // next==null说明当前w是尾结点,此时应该修改尾结点指针为当前节点的前导节点
                    if (next == null)
                         lastWaiter = trail;
               } else
                    trail = w;
               w = next;
          }
     }
}

 

posted @ 2021-04-23 22:05  榆木脑袋0v0  阅读(137)  评论(0)    收藏  举报