學習AQS的時候,了解到AQS依賴於內部的FIFO同步隊列來完成同步狀態的管理,當前線程獲取同步狀態失敗時,同步器會將當前線程以及等待狀態等信息構造成一個Node對象並將其加入到同步隊列,同時會阻塞當前線程,當同步狀態釋放時,會把首節點中的線程喚醒,使其再次嘗試獲取同步狀態。
這時,我有了一個疑問,AQS的同步隊列是FIFO的,就是先來排隊的先走。那怎么實現非公平鎖呢?查閱了一些資料,總算知道了。
首先從公平鎖開始看起。
ReentrantLock 的公平鎖
ReentrantLock 默認采用非公平鎖,除非在構造方法中傳入參數 true 。
//默認 public ReentrantLock() { sync = new NonfairSync(); } //傳入true or false public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
公平鎖的 lock 方法:
static final class FairSync extends Sync { final void lock() { acquire(1); } // AbstractQueuedSynchronizer.acquire(int arg) public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); } protected final boolean tryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { // 1. 和非公平鎖相比,這里多了一個判斷:是否有線程在等待 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; } }
我們可以看到,在注釋1的位置,有個!hasQueuedPredecessors()
條件,意思是說當前同步隊列沒有前驅節點(也就是沒有線程在等待)時才會去compareAndSetState(0, acquires)
使用CAS修改同步狀態變量。所以就實現了公平鎖,根據線程發出請求的順序獲取鎖。
非公平鎖的lock方法
static final class NonfairSync extends Sync { final void lock() { // 2. 和公平鎖相比,這里會直接先進行一次CAS,成功就返回了 if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } // AbstractQueuedSynchronizer.acquire(int arg) public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); } protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } } /** * Performs non-fair tryLock. tryAcquire is implemented in * subclasses, but both need nonfair try for trylock method. */ final boolean nonfairTryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { //3.這里也是直接CAS,沒有判斷前面是否還有節點。 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; }
非公平鎖的實現在剛進入lock方法時會直接使用一次CAS去嘗試獲取鎖,不成功才會到acquire方法中,如注釋2。而在nonfairTryAcquire方法中並沒有判斷是否有前驅節點在等待,直接CAS嘗試獲取鎖,如注釋3。由此實現了非公平鎖。
總結
非公平鎖和公平鎖的兩處不同:
-
非公平鎖在調用 lock 后,首先就會調用 CAS 進行一次搶鎖,如果這個時候恰巧鎖沒有被占用,那么直接就獲取到鎖返回了。
-
非公平鎖在 CAS 失敗后,和公平鎖一樣都會進入到 tryAcquire 方法,在 tryAcquire 方法中,如果發現鎖這個時候被釋放了(state == 0),非公平鎖會直接 CAS 搶鎖,但是公平鎖會判斷等待隊列是否有線程處於等待狀態,如果有則不去搶鎖,乖乖排到后面。
公平鎖和非公平鎖就這兩點區別,如果這兩次 CAS 都不成功,那么后面非公平鎖和公平鎖是一樣的,都要進入到阻塞隊列等待喚醒。
相對來說,非公平鎖會有更好的性能,因為它的吞吐量比較大。當然,非公平鎖讓獲取鎖的時間變得更加不確定,可能會導致在阻塞隊列中的線程長期處於飢餓狀態。
作者:小北覓
鏈接:https://www.jianshu.com/p/2ada27eee90b
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。