CountDownLatch概念
CountDownLatch又被称为倒计时锁,它是一个同步辅助类,它允许一个或多个线程一直等待直到其他线程执行完毕才开始执行。用给定的计数初始化CountDownLatch,其含义是要被等待执行完的线程个数。每次调用CountDown(),计数减1。
主程序执行到await()函数会阻塞等待线程的执行,直到计数为0
我们来看下它的方法。
public CountDownLatch(int count); //指定计数的次数,只能被设置1次
public void countDown(); //调用此方法则计数减1
public void await() throws InterruptedException //调用此方法会一直阻塞当前线程,直到计时器的值为0,除非线程被中断。
Public Long getCount(); //得到当前的计数
Public boolean await(long timeout, TimeUnit unit) //调用此方法会一直阻塞当前线程,直到计时器的值为0,除非线程被中断或者计数器超时,返回false代表计数器超时。
CountDownLatch实例
模拟10人赛跑。10人跑完后才喊"Game Over."
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class CountDownLatchTest {
private static final int RUNNER_COUNT = 10;
public static void main(String[] args) throws InterruptedException {
final CountDownLatch begin = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(10);
final ExecutorService exec = Executors.newFixedThreadPool(10);
for (int i = 0; i < RUNNER_COUNT; i++) {
final int NO = i + 1;
//从1号到10号线程都在这里被建立
Runnable run = new Runnable() {
@Override
public void run() {
try {
//所有线程阻塞在这里,等待枪响
begin.await();
//每个人跑完的时间
Thread.sleep((long)(Math.random() * 10000));
System.out.println("No." + NO + " arrived");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//每跑完一个人,倒计时锁减一
end.countDown();//(倒计时锁被减一)
}
}
};
exec.submit(run);
}
System.out.println("Game Start ...");
//枪响
begin.countDown();//倒计时锁被解除减一(begin解除)
//等待所有人跑完
end.await();//倒计时锁没有被解除之前,将被阻塞在这里
end.await(30, TimeUnit.SECONDS);
System.out.println("Game Over.");
exec.shutdown();
}
}
输出
Game Start ...
No.6 arrived
No.4 arrived
No.10 arrived
No.3 arrived
No.9 arrived
No.5 arrived
No.8 arrived
No.7 arrived
No.1 arrived
No.2 arrived
Game Over.
CountDownLatch与join的区别
调用join方法需要等待thread执行完毕才能继续向下执行,而CountDownLatch只需要检查计数器的值为零就可以继续向下执行,相比之下,CountDownLatch更加灵活一些,可以实现一些更加复杂的业务场景。
面试题
(1)解释一下CountDownLatch概念?
(2)CountDownLatch 和CyclicBarrier的不同之处?
(3)给出一些CountDownLatch使用的例子?
(4) CountDownLatch 类中主要的方法?