java.util.concurrent.CountDownLatch
CountDownLatch类可以实现,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
CountDownLatch是通过“共享锁”实现的。在创建CountDownLatch中时,会传递一个int类型参数count,该参数是“锁计数器”的初始状态,表示该“共享锁”最多能被count给线程同时获取。当某线程调用该CountDownLatch对象的await()方法时,该线程会等待“共享锁”可用时,才能获取“共享锁”进而继续运行。而“共享锁”可用的条件,就是“锁计数器”的值为0!而“锁计数器”的初始值为count,每当一个线程调用该CountDownLatch对象的countDown()方法时,才将“锁计数器”-1;通过这种方式,必须有count个线程调用countDown()之后,“锁计数器”才为0,而前面提到的等待线程才能继续运行。
1.类变量&常量
private final Sync sync; //提供所有实现机制的同步器
2.构造方法
public CountDownLatch(int count) {
//等待线程数不能小于0
if (count < 0) throw new IllegalArgumentException("count < 0");
this.sync = new Sync(count);
}
3.重要方法
1. await方法,调用acquireSharedInterruptibly,即可中断的共享状态的锁方法
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
2. 有时间限制的await方法,超时则不再等待
public boolean await(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
3. countDown方法,每调用一次就将标志位减1
public void countDown() {
sync.releaseShared(1);
}
4. getCount方法,获取当前等待的线程数
public long getCount() {
return sync.getCount();
}
4.内部类
private static final class Sync extends AbstractQueuedSynchronizer
继承自AQS类的内部类,是CountDownLatch的核心实现
//参数表示等待线程个数,将标志位设置成等待线程数
Sync(int count) {
setState(count);
}
//获取当前等待的线程数
int getCount() {
return getState();
}
//tryAcquireShared方法,如果线程数为0才返回1,即当前没有等待的线程。
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
//tryRelase方法,重写AQS方法,实现每被countDown调用,就将标志位-1,直到标志位为0
protected boolean tryReleaseShared(int releases) {
for (;;) {
int c = getState();
if (c == 0) //当为0时,即可“放行“
return false;
int nextc = c-1;
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}