使用方法
CountDownLatch一般都是配合线程池使用,可以用作等待X个线程执行完成在后在放行执行后面得逻辑。如果线程数量不足x个则会添加到同步队列,等待第x个执行countDown后唤醒同步队列中的线程。下面是CountDownLatch的一个例子:
public class CountDownLatchDemo {
private static final Integer POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
private static ExecutorService executor = new ThreadPoolExecutor(POOL_SIZE, POOL_SIZE * 2, 0, TimeUnit.MICROSECONDS,
new ArrayBlockingQueue<>(1000),new ThreadPoolExecutor.CallerRunsPolicy());;
public static void main(String[] args) {
//1.获取测试数据 每个subList有3条数据,一共20条;
List<List<String>> list = CollectionUtils.subList(getList(), 3);
list.forEach(subList -> {
int subListSize = subList.size();
//2.遍历list,给每个subList都new CountDownLatch且count值赋值为3,代表3条线程执行完成后一起执行后面得逻辑。
CountDownLatch countDownLatch = new CountDownLatch(3);
AtomicInteger index = new AtomicInteger();
subList.forEach(val -> {
index.incrementAndGet();
executor.execute(new OneThread(index.toString(), val, countDownLatch));
});
try {
//4.等线程执行全部执行完毕后执行的操作
countDownLatch.await();
System.out.println("----->>> " + subListSize + "个线程执行完毕后执行 <<<-----");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
private static List<String> getList() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 11; i++) {
list.add("a" + i);
}
return list;
}
public static class OneThread implements Runnable {
private String field1;
private String filed2;
private CountDownLatch countDownLatch;
public OneThread() {
}
public OneThread(String field1, String filed2, CountDownLatch countDownLatch) {
this.field1 = field1;
this.filed2 = filed2;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": filed1 -> " + field1 + " filed2 -> " + filed2 + " count -> " + countDownLatch.getCount());
//3.线程执行完后执行countDown方法。
countDownLatch.countDown();
}
}
}
执行结果如下:
pool-1-thread-2: filed1 -> 2 filed2 -> a1 count -> 3
pool-1-thread-1: filed1 -> 1 filed2 -> a0 count -> 3
pool-1-thread-3: filed1 -> 3 filed2 -> a2 count -> 3
----->>> 3个线程执行完毕后执行 <<<-----
pool-1-thread-4: filed1 -> 1 filed2 -> a3 count -> 3
pool-1-thread-5: filed1 -> 2 filed2 -> a4 count -> 3
pool-1-thread-6: filed1 -> 3 filed2 -> a5 count -> 1
----->>> 3个线程执行完毕后执行 <<<-----
pool-1-thread-7: filed1 -> 1 filed2 -> a6 count -> 3
pool-1-thread-8: filed1 -> 2 filed2 -> a7 count -> 2
pool-1-thread-9: filed1 -> 3 filed2 -> a8 count -> 1
----->>> 3个线程执行完毕后执行 <<<-----
pool-1-thread-10: filed1 -> 1 filed2 -> a9 count -> 3
pool-1-thread-11: filed1 -> 2 filed2 -> a10 count -> 2
可以看到前三次都能等待3个线程执行完毕后执行后面公共的逻辑,但是最后一次两个线程执行自身逻辑完成后无法在往后执行公共的逻辑,造成这个原因是什么呢,先说结论因为这两条线程被加到同步队列,此时state > 0 导致无法被唤醒。(一下源码都会以该案例举例)
源码分析
先看new CountDownLatch(3)中的3代表的含义
public CountDownLatch(int count) {
if (count < 0) throw new IllegalArgumentException("count < 0");
this.sync = new Sync(count);
}
Sync(int count) {
setState(count);
}
protected final void setState(int newState) {
//设置state值
state = newState;
}
/**
* The synchronization state.
*/
private volatile int state;
new CountDownLatch(3)其实就是给state赋值为3,此时AQS的state代表的是共享线程的数量。
/**
* Decrements the count of the latch, releasing all waiting threads if
* the count reaches zero.
*
* <p>If the current count is greater than zero then it is decremented.
* If the new count is zero then all waiting threads are re-enabled for
* thread scheduling purposes.
*
* <p>If the current count equals zero then nothing happens.
*/
public void countDown() {
sync.releaseShared(1);
}
/**
* Releases in shared mode. Implemented by unblocking one or more
* threads if {@link #tryReleaseShared} returns true.
*
* @param arg the release argument. This value is conveyed to
* {@link #tryReleaseShared} but is otherwise uninterpreted
* and can represent anything you like.
* @return the value returned from {@link #tryReleaseShared}
*/
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
执行countDown方法可以看到是执行sys.releaseShared方法,下面是对这两个方法的解析
protected boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
// 1. 获取state
int c = getState();
// 2.如果state = 0返回false
if (c == 0)
return false;
int nextc = c-1;
// 3. 通过CAS将state的值设置成state-1,返回sate==0的结果
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}
}
releaseShared方法解析:
- 当前两个线进入该方法,此时state > 0 且nextc > 0 CAS成功返回false
- 当第三条线程进入该方法,此时state > 0 且nextc==0 CAS成功返回true执行doReleaseShared方法
- 当第四条线程进入该方法,此时state == 0直接返回false
private void doReleaseShared() {
/*
* Ensure that a release propagates, even if there are other
* in-progress acquires/releases. This proceeds in the usual
* way of trying to unparkSuccessor of head if it needs
* signal. But if it does not, status is set to PROPAGATE to
* ensure that upon release, propagation continues.
* Additionally, we must loop in case a new node is added
* while we are doing this. Also, unlike other uses of
* unparkSuccessor, we need to know if CAS to reset status
* fails, if so rechecking.
*/
for (;;) {
// 1.获取同步队列的头节点
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
// 2.判断头节点状态是否为-1(有效)
if (ws == Node.SIGNAL) {
2.1. 有效则将头状态设置为0
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
2.2. 设置成功后唤醒头节点后面的线程
unparkSuccessor(h);
}
3. 判断头节点状态是否为0,为0则设置该状态为-3
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
// 4.如果头节点变动则继续循环,当最后一个node进入该判断此时 h == head一定为true结束循环
if (h == head) // loop if head changed
break;
}
}
/**
* Wakes up node's successor, if one exists.
*
* @param node the node
*/
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.
*/
2.3 将node节点状态设置为0
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.
*/
2.4 获取node节点的下一个节点
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
2.5 如果该s节点状态>0(无效)则获取队中的尾节点,往前遍历获取有效的节点并唤醒
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);
}
doReleaseShared方法解析:
- 第三条线程进入该方法,如果头节点状态为-1且CAS成功,会唤醒同步队列中挂起的线程,如果状态为0,则会设置为-2,进入下次循环,直到全部唤醒成功即 head == tail == node
接下来就是await方法解析:
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
// 1.判断线程是否被中断
if (Thread.interrupted())
throw new InterruptedException();
// 2. 判断state是否还剩余
if (tryAcquireShared(arg) < 0)
// 3. 还有剩余添加到同步队列
doAcquireSharedInterruptibly(arg);
}
await方法解析:
- 当前两条线程进入该方法,此时state > 0 ,添加到同步队列并且挂起等待唤醒(看吧这就与doReleaseShared方法串起来了,第三条线程唤醒前第一条线程,第一条又会唤醒第二条,一起往下执行)
- 当前第三条线程进入该方法,此时state == 0,await方法执行完成,往下继续执行
- 当前第四条线程进入该方法,此时state == 0,await方法执行完成,往下继续执行,可以看到第四条线程执行countDown与await其实啥也没干。
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
tryAcquireShared方法解析:
- 获取state值,判断该值是否为0
private void doAcquireSharedInterruptibly(int arg)
throws InterruptedException {
// 1. 新增共享Node,添加到队尾
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
for (;;) {
// 2.获取前驱节点,判断是否为头节点
final Node p = node.predecessor();
if (p == head) {
//3.获取state是否还有值与上面tryAcquireShared方法一样
int r = tryAcquireShared(arg);
if (r >= 0) {
// 4.设置当前节点为头节点,并且判断是否该唤醒下一个节点
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return;
}
}
// 5.不为头节点清理前驱节点并且挂起
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
//6.失败,将当前在同步队列中剔除
if (failed)
cancelAcquire(node);
}
}
private Node addWaiter(Node mode) {
// 1.1 new一个节点参数为当前线程与Node.SHARED
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
//1.2 获取尾节点,如果tail不为空CAS将node设置为尾节点。
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
// 1.3 为空执行enq
enq(node);
return node;
}
private Node enq(final Node node) {
for (;;) {
// 1.3.1 获取尾节点如果为空代表同步队列没有任何节点,new一个哨兵节点,并且头节点与尾部节点都指向它结束本次循环进入下一次循环
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))
tail = head;
} else {
// 1.3.2 获取尾节点,CAS将node设置为尾节点。
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
private void setHeadAndPropagate(Node node, int propagate) {
Node h = head; // Record old head for check below
// 4.1 将node设置为头节点
setHead(node);
/*
* Try to signal next queued node if:
* Propagation was indicated by caller,
* or was recorded (as h.waitStatus either before
* or after setHead) by a previous operation
* (note: this uses sign-check of waitStatus because
* PROPAGATE status may transition to SIGNAL.)
* and
* The next node is waiting in shared mode,
* or we don't know, because it appears null
*
* The conservatism in both of these checks may cause
* unnecessary wake-ups, but only when there are multiple
* racing acquires/releases, so most need signals now or soon
* anyway.
*/
if (propagate > 0 || h == null || h.waitStatus < 0 ||
(h = head) == null || h.waitStatus < 0) {
Node s = node.next;
if (s == null || s.isShared())
// 4.2 如果 propagate>0(state == 0) 并且下一个节点为共享节点,则唤醒后边的节点(与countDown的doReleaseShared一样)
doReleaseShared();
}
}
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
// 5.1 判断前驱节点的状态 -1 return true
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;
// 5.2 > 0 无效状态,循环判断前驱节点的节点,找到有效的节点,将当前节点的前驱节点设置为它。
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.
*/
// 5.3 如果是其他 < 0 的状态则设置为-1 (这又跟doReleaseShared方法联系到一块了,-3状态设置为-1,CAS成功唤醒其他节点)
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
// 6.1 自己看吧这其实就是将当前异常的节点在同步队列中剔除,判断了当前节点为tail节点 中间节点的情况
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;
// predNext is the apparent node to unsplice. CASes below will
// fail if not, in which case, we lost race vs another cancel
// or signal, so no further action is necessary.
Node predNext = pred.next;
// Can use unconditional write instead of CAS here.
// After this atomic step, other Nodes can skip past us.
// Before, we are free of interference from other threads.
node.waitStatus = Node.CANCELLED;
// If we are the tail, remove ourselves.
if (node == tail && compareAndSetTail(node, pred)) {
compareAndSetNext(pred, predNext, null);
} else {
// If successor needs signal, try to set pred's next-link
// so it will get one. Otherwise wake it up to propagate.
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
}
}
doAcquireSharedInterruptibly方法解析:
- 前两条线程进入该方法,此时state > 0 ,添加到同步队列,进入自旋,判断前驱节点是否为头节点,state是否为0;第三条线程进入countDown的doReleaseShared方法,此时头节点状态为-1且CAS成功,唤醒第一条线程,第一条线程doAcquireSharedInterruptibly方法中自旋成功,此时state == 0,第一条线程将自己设置为头节点,唤醒第二条线程。
- 第三条线程与第四条线程不进入该方法
结尾
通过源码可以看到,如果线程数量小于设置的state,则线程挂起之后无法被唤醒,所以设置state的值要根据实际情况来。回到标题为什么CountDownLatch不能复用呢?因为state值不能像Semaphore那样被加回去,如果复用的话state为0之后会一直为0,执行countDown与await等于就判断了下state的值,没有任何效果。