前言:為什么要了解AQS?
在如今有很多高并發的場景下,都免不了使用多線程,使用多線程就避免不了了解鎖。之前也提到了synchronized鎖(參見文章synchronized鎖),另一個常用的鎖就是ReentrantLock,而ReentrantLock底層實現就是AQS,當然還有很多其他的實現,接下來我們一起了解下。
一、AQS是什么?
AQS全稱:AbstractQueuedSynchronizer,抽象隊列式同步器。
AQS是一個抽象類,它定義了一套多線程訪問共享資源的同步器框架。通俗解釋,AQS就像是一個隊列管理員,當多線程操作時,對這些線程進行排隊管理。
AQS本身是一個抽象類,所以并沒有單獨實現什么功能,但是很多功能都繼承了AQS類,依賴于其底層支持。如:ReentrantLock、Semaphore、CountDownLatch、CycleBarrier。
二、AQS如何實現的?
AQS主要通過維護了兩個變量來實現同步機制的
2.1、state
AQS使用一個volatile修飾的私有變量來表示同步狀態,當state=0表示釋放了鎖,當state>0表示獲得鎖。
/**
* The synchronization state.
*/
private volatile int state;
另外,AQS提供了以下三個方法來對state進行操作。
protected final int getState() {
return state;
}
protected final void setState(int newState) {
state = newState;
}
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
2.2、FIFO同步隊列
AQS通過內置的FIFO同步隊列,來實現線程的排隊工作。
如果線程獲取當前同步狀態失敗,AQS會將當前線程的信息封裝成一個Node節點,加入同步隊列中,并且阻塞該線程,當同步狀態釋放,則會將隊列中的線程喚醒,重新嘗試獲取同步狀態。
static final class Node {
/** Marker to indicate a node is waiting in shared mode */
static final Node SHARED = new Node();
/** Marker to indicate a node is waiting in exclusive mode */
static final Node EXCLUSIVE = null;
/** waitStatus value to indicate thread has cancelled */
static final int CANCELLED = 1;
/** waitStatus value to indicate successor's thread needs unparking */
static final int SIGNAL = -1;
/** waitStatus value to indicate thread is waiting on condition */
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
static final int PROPAGATE = -3;
volatile int waitStatus;
volatile Node prev;
volatile Node next;
volatile Thread thread;
Node nextWaiter;
/**
* Returns true if node is waiting in shared mode.
*/
final boolean isShared() {
return nextWaiter == SHARED;
}
/**
* Returns previous node, or throws NullPointerException if null.
* Use when predecessor cannot be null. The null check could
* be elided, but is present to help the VM.
*
* @return the predecessor of this node
*/
final Node predecessor() throws NullPointerException {
Node p = prev;
if (p == null)
throw new NullPointerException();
else
return p;
}
Node() { // Used to establish initial head or SHARED marker
}
Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
}
Node(Thread thread, int waitStatus) { // Used by Condition
this.waitStatus = waitStatus;
this.thread = thread;
}
}
三、AQS一些特點
3.1 共享鎖和獨占鎖
AQS實現的獨占鎖有ReentrantLock,共享鎖有Semaphore,CountDownlatch,CycleBarrier。
3.1.1 要實現一個獨占鎖,需要重寫tryAcquire,tryRelease方法
Acquire: tryAcquire(嘗試獲取鎖)、addWaiter(入隊)、acquireQueued(隊列中的線程循環獲取鎖,失敗則掛起shouldParkAfterFailedAcquire)。
Release:tryRelease(嘗試釋放鎖)、unparkSuccessor(喚醒后繼節點)。
3.1.2 要實現共享鎖,需要重寫tryAcquireShared、tryReleaseShared
AcquireShared:tryAcquireShared,doAcquireShared。
RealseShared:tryRealseShared、doReleaseShared。
3.2 等待狀態位
CANCELLED = 1:因為超時或中斷,狀態位倍設置為取消,該線程不能去競爭鎖,也不能轉換為其他狀態;被檢測到之后會被踢出同步隊列,被GC回收。
SIGNAL = -1:該節點的后繼節點被阻塞,到時需要喚醒
CONDITION = -2:該節點在條件隊列中(condition),因為等待條件而阻塞。
PROPAGATE = -3:使用在共享模式的頭節點可能處于此狀態,表示鎖的下一次獲取可以無條件傳播。
0 :無狀態
四、了解AQS的整體流程
可見下面流程圖
五、AQS詳細分析
5.1 Sync.nonfairTryAcquire
nonfairTryAcquire方法將是lock方法間接調用的第一個方法,每次請求鎖時都會首先調用該方法。
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;
}
- 該方法會首先判斷當前狀態,如果c==0說明沒有線程正在競爭該鎖,如果不c !=0 說明有線程正擁有了該鎖。
- 如果發現c==0,則通過CAS設置該狀態值為acquires,acquires的初始調用值為1,每次線程重入該鎖都會+1,每次unlock都會-1,但為0時釋放鎖。如果CAS設置成功,則可以預計其他任何線程調用CAS都不會再成功,也就認為當前線程得到了該鎖,也作為Running線程,很顯然這個Running線程并未進入等待隊列。
- 如果c !=0 但發現自己已經擁有鎖,只是簡單地++acquires,并修改status值,但因為沒有競爭,所以通過setStatus修改,而非CAS,也就是說這段代碼實現了偏向鎖的功能,并且實現的非常漂亮。
5.2 AbstractQueuedSynchronizer.addWaiter
addWaiter方法負責把當前無法獲得鎖的線程包裝為一個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;
}
其中參數mode是獨占鎖還是共享鎖,默認為null,獨占鎖。追加到隊尾的動作分兩步:
- 如果當前隊尾已經存在(tail!=null),則使用CAS把當前線程更新為Tail。
- 如果當前Tail為null或則線程調用CAS設置隊尾失敗,則通過enq方法繼續設置Tail
下面是enq方法:
private Node enq(final Node node) {
for (;;) {
Node t = tail;
if (t == null) { // Must initialize
Node h = new Node(); // Dummy header
h.next = node;
node.prev = h;
if (compareAndSetHead(h)) {
tail = node;
return h;
}
}
else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
該方法就是循環調用CAS,即使有高并發的場景,無限循環將會最終成功把當前線程追加到隊尾(或設置隊頭)。總而言之,addWaiter的目的就是通過CAS把當前現在追加到隊尾,并返回包裝后的Node實例。
把線程要包裝為Node對象的主要原因,除了用Node構造供虛擬隊列外,還用Node包裝了各種線程狀態。
5.3 AbstractQueuedSynchronizer.acquireQueued
acquireQueued的主要作用是把已經追加到隊列的線程節點(addWaiter方法返回值)進行阻塞,但阻塞前又通過tryAccquire重試是否能獲得鎖,如果重試成功能則無需阻塞,這里是非公平鎖的由來之二
final boolean acquireQueued(final Node node, int arg) {
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} catch (RuntimeException ex) {
cancelAcquire(node);
throw ex;
}
}
仔細看看這個方法是個無限循環,感覺如果p == head && tryAcquire(arg)條件不滿足循環將永遠無法結束,當然不會出現死循環,奧秘在于第12行的parkAndCheckInterrupt會把當前線程掛起,從而阻塞住線程的調用棧。
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
如前面所述,LockSupport.park最終把線程交給系統(Linux)內核進行阻塞。當然也不是馬上把請求不到鎖的線程進行阻塞,還要檢查該線程的狀態,比如如果該線程處于Cancel狀態則沒有必要,具體的檢查在shouldParkAfterFailedAcquire中:
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park
*/
return true;
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
檢查原則在于:
- 規則1:如果前繼的節點狀態為SIGNAL,表明當前節點需要unpark,則返回成功,此時acquireQueued方法的第12行(parkAndCheckInterrupt)將導致線程阻塞
- 規則2:如果前繼節點狀態為CANCELLED(ws>0),說明前置節點已經被放棄,則回溯到一個非取消的前繼節點,返回false,acquireQueued方法的無限循環將遞歸調用該方法,直至規則1返回true,導致線程阻塞
- 規則3:如果前繼節點狀態為非SIGNAL、非CANCELLED,則設置前繼的狀態為SIGNAL,返回false后進入acquireQueued的無限循環,與規則2同
總體看來,shouldParkAfterFailedAcquire就是靠前繼節點判斷當前線程是否應該被阻塞,如果前繼節點處于CANCELLED狀態,則順便刪除這些節點重新構造隊列。
至此,鎖住線程的邏輯已經完成,下面討論解鎖的過程。
5.4. 解鎖
請求鎖不成功的線程會被掛起在acquireQueued方法的第12行,12行以后的代碼必須等線程被解鎖鎖才能執行,假如被阻塞的線程得到解鎖,則執行第13行,即設置interrupted = true,之后又進入無限循環。
從無限循環的代碼可以看出,并不是得到解鎖的線程一定能獲得鎖,必須在第6行中調用tryAccquire重新競爭,因為鎖是非公平的,有可能被新加入的線程獲得,從而導致剛被喚醒的線程再次被阻塞,這里充分體現了“非公平”。通過之后將要介紹的解鎖機制會看到,第一個被解鎖的線程就是Head,因此p == head的判斷基本都會成功。
解鎖代碼相對簡單,主要體現在AbstractQueuedSynchronizer.release和Sync.tryRelease方法中:
class AbstractQueuedSynchronizer
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
class Sync
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
tryRelease與tryAcquire語義相同,把如何釋放的邏輯延遲到子類中。tryRelease語義很明確:如果線程多次鎖定,則進行多次釋放,直至status==0則真正釋放鎖,所謂釋放鎖即設置status為0,因為無競爭所以沒有使用CAS。
release的語義在于:如果可以釋放鎖,則喚醒隊列第一個線程(Head),具體喚醒代碼如下:
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
* Thread to unpark is held in successor, which is normally
* just the next node. But if cancelled or apparently null,
* traverse backwards from tail to find the actual
* non-cancelled successor.
*/
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);
}
這段代碼的意思在于找出第一個可以unpark的線程,一般說來head.next == head,Head就是第一個線程,但Head.next可能被取消或被置為null,因此比較穩妥的辦法是從后往前找第一個可用線程。貌似回溯會導致性能降低,其實這個發生的幾率很小,所以不會有性能影響。之后便是通知系統內核繼續該線程,在Linux下是通過pthread_mutex_unlock完成。之后,被解鎖的線程進入上面所說的重新競爭狀態。