倒计时器
允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助。
一个 CountDownLatch
用给定的计数初始化。await()
方法阻塞,直到由于countDown()
方法的调用而导致当前计数达到零,之后所有等待线程被释放,并且任何后续的 await()
调用立即返回。 这是一个一次性的现象 - 计数无法重置。
构造函数
该类只有一个构造函数
public CountDownLatch(int count)
方法
public long getCount() //获取当前的计数
public void countDown() //减少当前计数
public void await() throws InterruptedException //使当前线程,等在倒计时器上,等待被唤醒
public boolean await(long timeout, TimeUnit unit) throws InterruptedException
示例
倒计时器是一次性的,不能重置,
public class CountDownLatchDemo implements Runnable{
static final CountDownLatch end = new CountDownLatch(10);
static final CountDownLatchDemo demo = new CountDownLatchDemo();
@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(10) * 1000);
System.out.println("check complete");
end.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(10);
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
service.submit(demo);
}
for (int i = 0; i < 3; i++) {
executorService.submit(()->{
try {
end.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("跳伞!");
});
}
}
}
运行结果如下:
check complete
check complete
check complete
check complete
check complete
check complete
check complete
check complete
check complete
check complete
跳伞!
跳伞!
跳伞!
循环栅栏
允许一组线程全部等待彼此达到共同屏障点的同步辅助。 循环阻塞在涉及固定大小的线程方的程序中很有用,这些线程必须偶尔等待彼此。 屏障被称为循环 ,因为它可以在等待的线程被释放之后重新使用。
构造方法
// parties表示计数总数
public CyclicBarrier(int parties)
// barrierAction 表示当计数器一次计数完成后,系统会执行的动作
public CyclicBarrier(int parties, Runnable barrierAction)
方法
// 等待所有 parties已经在这个障碍上调用
public int await()
// 查询这个障碍是否处于破碎状态。
public boolean isBroken()
// 将屏障重置为初始状态
public void reset()
示例
public class CyclicBarrierDemo {
public static class Student implements Runnable {
private String num;
private final CyclicBarrier cyclic;
public Student(String num, CyclicBarrier cyclic) {
this.num = num;
this.cyclic = cyclic;
}
@Override
public void run() {
try {
System.out.println(num + "号报道");
cyclic.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
public static class BarrierRun implements Runnable {
@Override
public void run() {
System.out.println("集合完毕,出发!");
}
}
public static void main(String[] args) {
final int N = 10;
Thread[] allStudent = new Thread[N];
CyclicBarrier cyclic = new CyclicBarrier(N, new BarrierRun());
System.out.println("集合!");
for (int i = 0; i < N; i++) {
allStudent[i] = new Thread(new Student("" + i, cyclic));
allStudent[i].start();
}
}
}
运行结果:
集合!
0号报道
1号报道
2号报道
3号报道
4号报道
5号报道
6号报道
7号报道
8号报道
9号报道
集合完毕,出发!