countDownLatch也是基於AQS,它是AQS共享功能的一個實現
1
countDownLatch構造
public CountDownLatch(int count) {
if (count < 0) throw new IllegalArgumentException("count < 0");
this.sync = new Sync(count);
}
count最終傳遞給state ,countDown也是對於state狀態的改變java
private static final class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 4982264981922014374L;node
Sync(int count) {
setState(count);
}app
int getCount() {
return getState();
}ui
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
}this
2 countDown實現線程
public void countDown() {
sync.releaseShared(1);
}code
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
protected boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
int c = getState();
if (c == 0)
return false;
int nextc = c-1;
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}orm
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);ip
/*
* 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);
}ci
3 await()實現 一、 public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } 二、 public final void acquireSharedInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) = 0) { // state的狀態是0,說明,countDown的全部任務已經完成 setHeadAndPropagate(node, r); //主線程所在的節點設置爲頭節點 p.next = null; // help GC failed = false; return; //主線程結束等待 } } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) throw new InterruptedException(); } } finally { if (failed) //若是是非正常退出的話,取消 cancelAcquire(node); } } private void setHeadAndPropagate(Node node, int propagate) { Node h = head; // Record old head for check below setHead(node); if (propagate > 0 || h == null || h.waitStatus 0) { do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.next = node; } else { //講等待狀態設置爲後繼喚醒 compareAndSetWaitStatus(pred, ws, Node.SIGNAL); } return false; } // 當前線程阻塞,判斷線程是否中斷 private final boolean parkAndCheckInterrupt() { LockSupport.park(this); return Thread.interrupted(); } //取消當前節點獲取鎖 private void cancelAcquire(Node node) { // Ignore if node doesn't exist if (node == null) return; node.thread = null; // Skip cancelled predecessors Node pred = node.prev; while (pred.waitStatus > 0) node.prev = pred = pred.prev; Node predNext = pred.next; //若是節點都沒有被取消的話,那麼這個節點和node是同一個節點 //node的後繼節點取消 node.waitStatus = Node.CANCELLED; // If we are the tail, remove ourselves. // CountDownLatch 邏輯就到這裏 if (node == tail && compareAndSetTail(node, pred)) { compareAndSetNext(pred, predNext, null); } else { int ws; if (pred != head && ((ws = pred.waitStatus) == Node.SIGNAL || (ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) && pred.thread != null) { Node next = node.next; if (next != null && next.waitStatus <= 0) compareAndSetNext(pred, predNext, next); } else { unparkSuccessor(node); } node.next = node; // help GC } } countdownlatch經過檢查state,是否爲0 ,判斷全部任務是否已經完成