久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

tryAcquire()、addWaiter()、acquireQueued()三者的區(qū)別是什么

tryAcquire()、addWaiter()、acquireQueued()三者的區(qū)別是什么,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)建站專注于遂平網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供遂平營銷型網(wǎng)站建設(shè),遂平網(wǎng)站制作、遂平網(wǎng)頁設(shè)計(jì)、遂平網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造遂平網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供遂平網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

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方法的前半部分,通過CAS操作將state的值從0變?yōu)?,否則判斷當(dāng)前線程是否為exclusiveOwnerThread,然后把state++,也就是重入鎖的體現(xiàn),我們注意前半部分是通過CAS來保證同步,后半部分并沒有同步的體現(xiàn),原因是:后半部分是線程重入,再次獲得鎖時(shí)才觸發(fā)的操作,此時(shí)當(dāng)前線程擁有鎖,所以對(duì)ReentrantLock的屬性操作是無需加鎖的。如果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來達(dá)到阻塞效果;

關(guān)于tryAcquire()、addWaiter()、acquireQueued()三者的區(qū)別是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)頁名稱:tryAcquire()、addWaiter()、acquireQueued()三者的區(qū)別是什么
本文地址:http://sd-ha.com/article12/gcjigc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、小程序開發(fā)、網(wǎng)站收錄、用戶體驗(yàn)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)