接下來(lái)跟著上一篇, 解析情景三和情景四
情景三
讀寫(xiě)讀
線程1的獲得讀鎖的操作和情景一相同, 接下來(lái)從線程2開(kāi)始分析
public void lock() {
sync.acquire(1);
}
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
protected final boolean tryAcquire(int acquires) {
// thread-1
Thread current = Thread.currentThread();\
// c = 65536
int c = getState();
// w = 0
int w = exclusiveCount(c);
if (c != 0) {
// 進(jìn)入此分支
if (w == 0 || current != getExclusiveOwnerThread())
return false;
......
}
......
}
之后返回acquire方法
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
// 最后thread-1將自己加入隊(duì)列并阻塞
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
此時(shí)線程2開(kāi)始執(zhí)行
// ReadLock
public void lock() {
sync.acquireShared(1);
}
public final void acquireShared(int arg) {
if (tryAcquireShared(arg) < 0)
doAcquireShared(arg);
}
protected final int tryAcquireShared(int unused) {
// thread-2
Thread current = Thread.currentThread();
// c = 65536
int c = getState();
// 由于線程1(寫(xiě)操作)被阻塞, 沒(méi)有獲取寫(xiě)鎖
// 不走此分支
if (exclusiveCount(c) != 0 &&
getExclusiveOwnerThread() != current)
return -1;
// r = 1
int r = sharedCount(c);
if (!readerShouldBlock() &&
r < MAX_COUNT &&
compareAndSetState(c, c + SHARED_UNIT)) {
......
}
return 1;
}
......
}
static final class FairSync extends Sync {
final boolean readerShouldBlock() {
return hasQueuedPredecessors();
}
}
public final boolean hasQueuedPredecessors() {
Node t = tail;
Node h = head;
Node s;
// 由于thread-1被阻塞, 所以head和tail不相等
return h != t &&
// 隊(duì)列中的末尾是thread-1, 非當(dāng)前線程thread-2
// 返回true
((s = h.next) == null || s.thread != Thread.currentThread());
}
說(shuō)明在隊(duì)列中還有排隊(duì)的線程, 返回tryAcquireShared
protected final int tryAcquireShared(int unused) {
Thread current = Thread.currentThread();
int c = getState();
if (exclusiveCount(c) != 0 &&
getExclusiveOwnerThread() != current)
return -1;
int r = sharedCount(c);
// 此讀線程應(yīng)該被阻塞
if (!readerShouldBlock() &&
r < MAX_COUNT &&
compareAndSetState(c, c + SHARED_UNIT)) {
......
}
// 走此分支
return fullTryAcquireShared(current);
}
final int fullTryAcquireShared(Thread current) {
HoldCounter rh = null;
for (;;) {
// c = 65536
int c = getState();
// 此時(shí)希望獲取寫(xiě)鎖的線程二被阻塞, 不走此分支
if (exclusiveCount(c) != 0) {
if (getExclusiveOwnerThread() != current)
return -1;
}
// 由于此時(shí)隊(duì)列中有線程1, 所以可以進(jìn)入此分支
else if (readerShouldBlock()) {
// firstReader是線程1, 當(dāng)前線程為線程3
// 不走此分支
if (firstReader == current) {
// assert firstReaderHoldCount > 0;
}
else {
if (rh == null) {
// 由于只有線程1獲得讀鎖
// 所以cachedHoldCounter為null
rh = cachedHoldCounter;
// 進(jìn)入此分支
if (rh == null || rh.tid != getThreadId(current)) {
// 創(chuàng)建HoldCounter對(duì)象(對(duì)應(yīng)當(dāng)前線程)
rh = readHolds.get();
// 此時(shí)當(dāng)前線程沒(méi)有重新獲得讀鎖
if (rh.count == 0)
// 刪除當(dāng)前線程對(duì)應(yīng)的
readHolds.remove();
}
}
// 返回-1
if (rh.count == 0)
return -1;
}
}
......
}
}
回到acquireShared方法
public final void acquireShared(int arg) {
// tryAcquireShared最終返回-1
// 進(jìn)入該分支
if (tryAcquireShared(arg) < 0)
doAcquireShared(arg);
}
private void doAcquireShared(int arg) {
// 將代表當(dāng)前線程的node加入隊(duì)列中
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
// p代表線程1
final Node p = node.predecessor();
// p與head是不相等的, 不走此分支
if (p == head) {
......
}
// 第一次循環(huán)不會(huì)阻塞
// 第二次循環(huán)會(huì)到parkAndCheckInterrupt被阻塞
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
情景四
讀寫(xiě)寫(xiě)
和情景三類似, 只不過(guò)是線程三是寫(xiě)操作
// WriteLock
public void lock() {
sync.acquire(1);
}
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
protected final boolean tryAcquire(int acquires) {
// current為thread-2
Thread current = Thread.currentThread();
// c = 65536
int c = getState();
// w = 0
int w = exclusiveCount(c);
if (c != 0) {
// 進(jìn)入此分支
if (w == 0 || current != getExclusiveOwnerThread())
return false;
......
}
......
}
回到acquire方法中
public final void acquire(int arg) {
// 由于tryAcquire返回false
if (!tryAcquire(arg) &&
// 進(jìn)入此分支
// 創(chuàng)建代表當(dāng)前線程的節(jié)點(diǎn)
// 并加入隊(duì)列后被阻塞
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}