閱讀優(yōu)秀的源碼是提升編程技巧的重要手段之一。
如有不對的地方,歡迎指正
轉(zhuǎn)載請注明出處https://blog.lzoro.com。
碎碎念
如果需要使用或者了解ReentrantLock,證明已經(jīng)步入并發(fā)編程領(lǐng)域了,這里理論基礎(chǔ)不多提,需要的自行查閱資料。
但是,相關(guān)術(shù)語還是要做一下描述的。
ReentrantLock:可重入鎖
AQS:AbstractQueuedSynchronized 抽象類,隊列式同步器
CAS:Compare and Swap, 比較并交換值
CLH隊列:The wait queue is a variant of a "CLH" (Craig, Landin, and
* Hagersten) lock queue.
ReentrantLock
首先,貼圖大家感受一下。


其中Sync是ReentrantLock的抽象靜態(tài)內(nèi)部類,提供了鎖的同步措施,具體實現(xiàn)有NonFairSync和FairSync,分別為公平和非公平鎖。
從圖中我們可以看出,ReentrantLock是實現(xiàn)了Lock接口和Serializable接口,Serializable是Java的序列化接口,這里我們不多做討論。
那么,開始源碼的閱讀了~
首先,先看下Lock接口提供的方法(篇幅所限,這里將源碼注釋去掉),大致可分為三類:獲取鎖、釋放鎖、新建條件(可用于高級應(yīng)用,如等待/喚醒)。
public interface Lock {
/**
* 獲取鎖,若獲取失敗則進行等待
*/
void lock();
/**
* 可中斷鎖
*/
void lockInterruptibly() throws InterruptedException;
/**
* 獲取鎖,立即返回,成功返回true,否則false
*/
boolean tryLock();
/**
* 獲取鎖,若獲取失敗則在指定時間內(nèi)等待,成功返回true,否則false
*/
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
/**
* 釋放鎖
*/
void unlock();
/**
* 新建條件,可用與高級應(yīng)用
*/
Condition newCondition();
}
接下來我們具體看下ReentrantLock的實現(xiàn)。
public void lock() {
sync.lock();
}
可以看到ReentrantLock的lock方法,是調(diào)用靜態(tài)內(nèi)部類sysc的lock方法的,而sync的lock方法是抽象方法,具體的實現(xiàn)有兩個,NonfairSync(非公平鎖)和FairSync(公平鎖),我們先來看NonFairSync的實現(xiàn)
final void lock() {
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
compareAndSetState(0,1)這個方法是由sysn的父類AbstractQueuedSynchronizer來實現(xiàn)的,也是我們通常說的AQS,而compareAndSetState方法的具體實現(xiàn)是由Unsafe提供的。
Unfafe類的compareAndSwap*系列方法,是虛擬機的本地方法實現(xiàn),具體的實現(xiàn)不在我們的討論范圍內(nèi),簡單介紹一下作用,該方法的的作用如下:調(diào)用該方法時,若value值與expect值相等,則將value修改為update值,并返回true;若value值與expect值不相等,那么不做任何操作,并返回false,這也就是我們常說的CAS操作,至于存在的ABA等問題和解決方案,有興趣的可以自己搜索資料。
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
lock方法執(zhí)行完CAS操作后
若得的一個true返回,則會執(zhí)行setExclusiveOwnerThread(Thread.currentThread());,該方法作用是為鎖設(shè)置獨占線程,其實也就是一個賦值操作,如下:
protected final void setExclusiveOwnerThread(Thread thread) {
exclusiveOwnerThread = thread;
}
若CAS操作返回一個false,則執(zhí)行acquire方法
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
從上面源碼可以看出,若tryAcquire失敗并且acquireQueued返回true中斷標(biāo)識的話,將會中斷當(dāng)前線程。
我們先看一下tryAcquire,方法的作用大致如下:判斷鎖的state值,若當(dāng)前未有其他線程持有該鎖,則執(zhí)行CAS操作,成功后則設(shè)置獨占線程;若發(fā)現(xiàn)該鎖已被線程持有,則判斷持有線程是不是當(dāng)前線程,若是則允許重入,并判斷重入的次數(shù)是否超過限制,重入成功后修改state并返回一個true布爾值。
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
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;
}
接下來看一下acquireQueued和addWaiter方法,作用描述如下,利用addWaiter方法,將當(dāng)前線程作為一個節(jié)點Node加入的CLH隊列(The wait queue is a variant of a "CLH" (Craig, Landin, and
* Hagersten) lock queue),這里加入隊列有一系列操作,包括尾部判斷,前置節(jié)點設(shè)置等,成功加入隊列后,會判斷是否有資格去競爭獲取鎖,有則嘗試獲取鎖,成功后會返回標(biāo)志位。如果沒有資格,則判斷是否可以被阻塞,并做相關(guān)操作,具體請看注釋。
final boolean acquireQueued(final Node node, int arg) {
//失敗標(biāo)志
boolean failed = true;
try {
//是否中斷標(biāo)志
boolean interrupted = false;
for (;;) {
//獲取前置節(jié)點
final Node p = node.predecessor();
//如果前置節(jié)點為首節(jié)點,并且當(dāng)前線程能夠成功獲取鎖
if (p == head && tryAcquire(arg)) {
//將當(dāng)前節(jié)點設(shè)置為首節(jié)點
setHead(node);
p.next = null; //help GC,前首節(jié)點出隊,幫助GC
failed = false;
return interrupted;
}
//判斷是否可以阻塞線程并做相應(yīng)操作,下面具體閱讀這幾個方法
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
//判斷是否獲取失敗
if (failed)
cancelAcquire(node);
}
}
private Node addWaiter(Node mode) {
//封裝成node
Node node = new Node(Thread.currentThread(), mode);
// 獲取隊列尾節(jié)點(作為當(dāng)前節(jié)點的前置節(jié)點)
Node pred = tail;
//如果尾節(jié)點不為空
if (pred != null) {
//設(shè)置當(dāng)前節(jié)點的前置節(jié)點
node.prev = pred;
//CAS操作,設(shè)置隊列尾部
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
//尾節(jié)點為null,調(diào)用enq方法
enq(node);
//返回當(dāng)前節(jié)點
return node;
}
private Node enq(final Node node) {
for (;;) {
Node t = tail;
//尾節(jié)點為null
if (t == null) { // Must initialize
//通過CAS操作設(shè)置首節(jié)點
if (compareAndSetHead(new Node()))
//將首節(jié)點賦值給尾節(jié)點(初始化)
tail = head;
} else {
//設(shè)置當(dāng)前節(jié)點的前置節(jié)點
node.prev = t;
//通過CAS操作設(shè)置尾節(jié)點
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
接下來是shouldParkAfterFailedAcquire和parkAndCheckInterrupt方法
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
//獲取前置節(jié)點的等待狀態(tài)
int ws = pred.waitStatus;
//如果為SIGNAL
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
//只有當(dāng)前置節(jié)點的狀態(tài)位SIGNAL的話,當(dāng)前節(jié)點才能進入阻塞,并等待前置節(jié)點的喚醒
return true;
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
//如果前置節(jié)點為取消狀態(tài),則不斷往前搜索并找到SIGNAL狀態(tài)的節(jié)點,并加在其后面
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.
*/
//通過CAS操作設(shè)置前置節(jié)點的等待狀態(tài)位SIGNAL
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
/**
* 如果上面的方法調(diào)用返回true,則代表當(dāng)前節(jié)點可以進入阻塞/等待
*/
private final boolean parkAndCheckInterrupt() {
//通過LockSupport類的park方法來阻塞當(dāng)前線程
LockSupport.park(this);
//被喚醒后,返回中斷標(biāo)志
return Thread.interrupted();
}
/**
* 這里的阻塞具體實現(xiàn)是JVM虛擬機的本地實現(xiàn),有興趣者可以自行研究
*/
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(false, 0L);
setBlocker(t, null);
}
看到這里,會有點困惑,如果沒有成功獲取到鎖的線程進入了阻塞狀態(tài),那么它什么時候被喚醒呢?
這里有一個不得不提的點,如果使用lock方法來進行加鎖,那么必須成對地使用unlock來釋放鎖,否則容易導(dǎo)致死鎖,一般都是在try-catch-finally進行鎖的釋放。
所以,等待線程的被喚醒是由持有鎖的線程調(diào)用unlock后觸發(fā)的。
接下來,從unlock入手來具體看下源碼,可以看到unlock方法是調(diào)用sync.release(1)實現(xiàn)的,還是以開頭的NonFairSync(非公平鎖)的實現(xiàn)來看,
① 解鎖
public void unlock() {
sync.release(1);
}
② 釋放鎖
public final boolean release(int arg) {
//判斷是否釋放成功
if (tryRelease(arg)) {
Node h = head;
//判斷CLH隊列的首節(jié)點是否為null,并判斷等待狀態(tài)是否正常
if (h != null && h.waitStatus != 0)
//喚醒節(jié)點
unparkSuccessor(h);
return true;
}
return false;
}
③ 釋放鎖,并喚醒CLH隊列中的合法首節(jié)點
protected final boolean tryRelease(int releases) {
//計算state和釋放數(shù)量的差值
int c = getState() - releases;
//判斷線程是否是鎖持有者
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
//初始化釋放結(jié)果
boolean free = false;
//如果當(dāng)前線程未重入,釋放成功
if (c == 0) {
free = true;
//釋放鎖持有的線程
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
④ 喚醒阻塞/等待的節(jié)點
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.
*/
//獲取節(jié)點等待狀態(tài)
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.
*/
//獲取后置節(jié)點
Node s = node.next;
//后置節(jié)點為null或者為取消狀態(tài)
if (s == null || s.waitStatus > 0) {
s = null;
//從尾部向前獲取到一個不為null且狀態(tài)不是取消的節(jié)點
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
//喚醒該節(jié)點
if (s != null)
LockSupport.unpark(s.thread);
}
被喚醒后的節(jié)點,返回是否被中斷過的標(biāo)志,在acquireQueued方法內(nèi)繼續(xù)執(zhí)行循環(huán)獲取鎖的流程。
到這里,NonfairSync非公平鎖的分析基本上就告一段落了,而關(guān)于FairSync的公平機制,有興趣的可以去閱讀下,實現(xiàn)的機制大同小異。
以上,就是Java可重入鎖ReentrantLock的lock和unLock源碼分析,膜拜Java源碼大神。
總結(jié)
1、Lock提供lock、lockInterruptibly、tryLock()、tryLock(long time, TimeUnit unit)、unlock、newCondition五個方法;
2、lock和unlock必須成對調(diào)用;
3、ReentrantLock實現(xiàn)了Lock和Serializable兩個接口;
4、Sync是ReentrantLock的靜態(tài)內(nèi)部類,提供了公平鎖(FairSync)和非公平鎖(NonFairSync)的實現(xiàn)。
5、CAS操作是基于JVM提供的本地方法實現(xiàn)。
6、待補充
賣萌
喜歡的不妨給個贊唄,溜了溜了。