CountDownLatch

package juc.test;
/**
 * 可以在闭锁上等待,每次执行countdown后,count-1,当count=0时,等待的线程被唤醒
 */
public class CountDownLatch {
    /**
     * 内部类,使用AQS同步,利用AQS的state做计数count
     */
    private static final class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 4982264981922014374L;

        Sync(int count) {
            setState(count);
        }

        int getCount() {
            return getState();
        }

        /*
         * count=0请求成功;否则,请求失败,请求线程将被阻塞
         * 闭锁只有共享模式
         */
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }
        /*
         * 释放
         */
        protected boolean tryReleaseShared(int releases) {
            // Decrement count; signal when transition to zero
            for (;;) {
                int c = getState();
                if (c == 0) // 闭锁已经是0,无需释放
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc)) // CAS修改count=count-1
                    // 如果修改后为0表示闭锁已经打开,返回true;否则,说明还有等待的,闭锁处于关闭状态,返回false
                    return nextc == 0;
            }
        }
    }

    private final Sync sync;

    /*
     * 根据给定count值构造闭锁
     */
    public CountDownLatch(int count) {
        if (count < 0) throw new IllegalArgumentException("count < 0");
        this.sync = new Sync(count);
    }

     /* 
      * 如果当前count为0,那么方法立即返回。 
      * 如果当前count不为0,那么当前线程会一直等待,直到count被(其他线程)减到0或者当前线程被中断。 
      */  
    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1); // AQS中的实现
    }

    /*
     * 超时版await
     */
    public boolean await(long timeout, TimeUnit unit)
        throws InterruptedException {
        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
    }

    /*
     * 递减count,当减到0时,唤醒所有等待的线程去调度。如果当前count是0,则什么都不会发生
     */
    public void countDown() {
        sync.releaseShared(1);
    }

    public long getCount() {
        return sync.getCount();
    }

    public String toString() {
        return super.toString() + "[Count = " + sync.getCount() + "]";
    }
}