釋放鎖的核心函數(shù)release:
public final boolean release(int arg) {
if (tryRelease(arg)) {//同tryAcquire具體實(shí)現(xiàn)類有自己實(shí)現(xiàn),后面看reentrantlock的實(shí)現(xiàn)
Node h = head;
if (h != null && h.waitStatus != 0)//head=null的情況只有一個(gè)線程進(jìn)入,沒有初始化隊(duì)列,!=null至少說明隊(duì)列被初始化過,但是是否有后續(xù)節(jié)點(diǎn)未知,waitStatus!=0說明下個(gè)節(jié)點(diǎn)是等待的
unparkSuccessor(h);//喚醒下個(gè)節(jié)點(diǎn)
return true;
}
return false;
}
tryRelease比較簡(jiǎn)單,比較state的值是否減到0
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;
}
private void unparkSuccessor(Node node) {
int ws = node.waitStatus;
//ws小于0表示正常排隊(duì)線程,先設(shè)置為0
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
Node s = node.next;
//這里著實(shí)沒想出什么時(shí)候s會(huì)null,除了手動(dòng)去修改隊(duì)列,我理解只是作者的保護(hù)
//waitStatus>0表示下個(gè)線程是被cancel狀態(tài)
//進(jìn)這個(gè)是從隊(duì)尾開始找,找最近的正常排隊(duì)的線程
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);//喚醒下一個(gè)等待隊(duì)列中的線程
}