使用示例
public class Test {
public static void main(String[] args) throws Exception{
CountDownLatch count = new CountDownLatch(3); //倒计时器
for(int i=1; i<=3; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName());
count.countDown(); //减一
}, "Thread-"+i).start();
}
count.await(); //count归零后才能继续执行下去
System.out.println("结束!");
}
}
结果:
Thread-1
Thread-3
Thread-2
结束!