CountDownLatch允许一个或多个线程等待其他线程完成操作。
    CountDownLatch的构造函数接收一个int类型的参数作为计数器,当我们调用countDown方法时,计数器会减1,await方法会阻塞当前线程,直到计数器变成0。

    1. //countDown最后的执行逻辑
    2. protected boolean tryReleaseShared(int releases) {
    3. // Decrement count; signal when transition to zero
    4. for (;;) {
    5. int c = getState();
    6. if (c == 0)
    7. return false;
    8. int nextc = c-1;
    9. if (compareAndSetState(c, nextc))
    10. return nextc == 0;
    11. }
    12. }
    1. public final void acquireSharedInterruptibly(int arg)
    2. throws InterruptedException {
    3. //相应中断
    4. if (Thread.interrupted())
    5. throw new InterruptedException();
    6. if (tryAcquireShared(arg) < 0)
    7. doAcquireSharedInterruptibly(arg);
    8. }
    9. protected int tryAcquireShared(int acquires) {
    10. return (getState() == 0) ? 1 : -1;
    11. }