溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何理解非公平鎖與公平鎖

發(fā)布時間:2021-10-23 15:39:56 來源:億速云 閱讀:164 作者:iii 欄目:編程語言

這篇文章主要講解了“如何理解非公平鎖與公平鎖”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何理解非公平鎖與公平鎖”吧!

公平鎖與非公平鎖的一個重要區(qū)別就在于上圖中的2、6、10那個步驟,對應源碼如下:

//非公平鎖
 final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
              //區(qū)別重點看這里
                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;
        }

  //公平鎖
  protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
              //hasQueuedPredecessors這個方法就是最大區(qū)別所在
                if (!hasQueuedPredecessors() &&
                    compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0)
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }

分析以上代碼,我們可以看到公平鎖就是在獲取鎖之前會先判斷等待隊列是否為空或者自己是否位于隊列頭部,該條件通過才能繼續(xù)獲取鎖。
在結(jié)合兔子喝水的圖分析,非公平鎖獲取所得順序基本決定在9、10、11這三個事件發(fā)生的先后順序,
1、若在釋放鎖的時候總是沒有新的兔子來打擾,則非公平鎖等于公平鎖;
2、若釋放鎖的時候,正好一個兔子來喝水,而此時位于隊列頭的兔子還沒有被喚醒(因為線程上下文切換是需要不少開銷的),此時后來的兔子則優(yōu)先獲得鎖,成功打破公平,成為非公平鎖;

其實對于非公平鎖,只要線程進入了等待隊列,隊列里面依然是FIFO的原則,跟公平鎖的順序是一樣的。因為公平鎖與非公平鎖的release()部分代碼是共用AQS的代碼。

private void unparkSuccessor(Node node) {
        int ws = node.waitStatus;
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);

        Node s = node.next;
        if (s == null || s.waitStatus > 0) {
            s = null;
            for (Node t = tail; t != null && t != node; t = t.prev)
                if (t.waitStatus <= 0)
                    s = t;
        }
        if (s != null)
           //喚醒隊列頭的線程
            LockSupport.unpark(s.thread);
    }

感謝各位的閱讀,以上就是“如何理解非公平鎖與公平鎖”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對如何理解非公平鎖與公平鎖這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI