亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解tryAcquire()、addWaiter()、acquireQueued()

 更新時(shí)間:2019年03月13日 15:51:12   作者:胖虎。。  
這篇文章主要tryAcquire()、addWaiter()、acquireQueued()的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文實(shí)例為大家分享了tryAcquire()、addWaiter()、acquireQueued()的用法 ,供大家參考,具體內(nèi)容如下

tryAcquire()

final boolean nonfairTryAcquire(int acquires) {
      final Thread current = Thread.currentThread();
      int c = getState();
      if (c == 0) {
        if (compareAndSetState(0, acquires)) {
          setExclusiveOwnerThread(current);
          return true;
        }
      }
      else if (current == getExclusiveOwnerThread()) {
        int nextc = c + acquires;
        if (nextc < 0) // overflow
          throw new Error("Maximum lock count exceeded");
        setState(nextc);
        return true;
      }
      return false;
    }

先判斷state是否為0,如果為0就執(zhí)行上面提到的lock方法的前半部分,通過(guò)CAS操作將state的值從0變?yōu)?,否則判斷當(dāng)前線程是否為exclusiveOwnerThread,然后把state++,也就是重入鎖的體現(xiàn),我們注意前半部分是通過(guò)CAS來(lái)保證同步,后半部分并沒(méi)有同步的體現(xiàn),原因是:后半部分是線程重入,再次獲得鎖時(shí)才觸發(fā)的操作,此時(shí)當(dāng)前線程擁有鎖,所以對(duì)ReentrantLock的屬性操作是無(wú)需加鎖的。如果tryAcquire()獲取失敗,則要執(zhí)行addWaiter()向等待隊(duì)列中添加一個(gè)獨(dú)占模式的節(jié)點(diǎn)。

addWaiter()

/**
   * Creates and enqueues node for current thread and given mode.
   *
   * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
   * @return the new 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;
  }

這個(gè)方法的注釋:創(chuàng)建一個(gè)入隊(duì)node為當(dāng)前線程,Node.EXCLUSIVE 是獨(dú)占鎖, Node.SHARED 是共享鎖。
先找到等待隊(duì)列的tail節(jié)點(diǎn)pred,如果pred!=null,就把當(dāng)前線程添加到pred后面進(jìn)入等待隊(duì)列,如果不存在tail節(jié)點(diǎn)執(zhí)行enq()

private Node enq(final Node node) {
    for (;;) {
      Node t = tail;
      if (t == null) { // Must initialize
        if (compareAndSetHead(new Node()))
          tail = head;
      } else {
        node.prev = t;
        if (compareAndSetTail(t, node)) {
          t.next = node;
          return t;
        }
      }
    }
  }

這里進(jìn)行了循環(huán),如果此時(shí)存在了tail就執(zhí)行同上一步驟的添加隊(duì)尾操作,如果依然不存在,就把當(dāng)前線程作為head結(jié)點(diǎn)。
插入節(jié)點(diǎn)后,調(diào)用acquireQueued()進(jìn)行阻塞

acquireQueued()

final boolean acquireQueued(final Node node, int arg) {
    boolean failed = true;
    try {
      boolean interrupted = false;
      for (;;) {
        final Node p = node.predecessor();
        if (p == head && tryAcquire(arg)) {
          setHead(node);
          p.next = null; // help GC
          failed = false;
          return interrupted;
        }
        if (shouldParkAfterFailedAcquire(p, node) &&
          parkAndCheckInterrupt())
          interrupted = true;
      }
    } finally {
      if (failed)
        cancelAcquire(node);
    }
  }

先獲取當(dāng)前節(jié)點(diǎn)的前一節(jié)點(diǎn)p,如果p是head的話就再進(jìn)行一次tryAcquire(arg)操作,如果成功就返回,否則就執(zhí)行shouldParkAfterFailedAcquire、parkAndCheckInterrupt來(lái)達(dá)到阻塞效果;

以上所述是小編給大家介紹的tryAcquire()、addWaiter()、acquireQueued()的用法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論