码迷,mamicode.com
首页 > 其他好文 > 详细

AbstractQueuedSynchronizer类方法

时间:2020-09-14 19:10:37      阅读:24      评论:0      收藏:0      [点我收藏+]

标签:rup   queue   wait   ack   bst   ble   fail   bool   ast   

源码:

     public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
             //子类方法
             return tryAcquireShared(arg) >= 0 ||  doAcquireSharedNanos(arg, nanosTimeout);
         }
      /** * Acquires in shared interruptible mode. * @param arg the acquire argument */
      private void doAcquireSharedInterruptibly(int arg)   
         throws InterruptedException {   
              final Node node = addWaiter(Node.SHARED);  
              boolean failed = true;   
              try {      
                        for (;;) {           
                          final Node p = node.predecessor();           
                             if (p == head) {               
                               int r = tryAcquireShared(arg);               
                               if (r >= 0) {                   
                                 setHeadAndPropagate(node, r);                   
                                 p.next = null;
                                 // help GC     
                                 failed = false;                  
                                 return;               
                                   }           
                                }           
                                if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())               
                                    throw new InterruptedException();       
                                 }   
                          } finally {       
                             if (failed)           
                                cancelAcquire(node);   
                         }
                      }
                  }
               }
           }
       }

 

  添加等待

  private Node addWaiter(Node mode) {
        Node node = new Node(Thread.currentThread(), mode);
        // Try the fast path of enq; backup to full enq on failure
        Node pred = tail;
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        enq(node);
        return node;
    }

 

AbstractQueuedSynchronizer类方法

标签:rup   queue   wait   ack   bst   ble   fail   bool   ast   

原文地址:https://www.cnblogs.com/HuiShouGuoQu/p/13596960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!