public class CountDownLatchDemo {
//任务1-6做完后,打印:任务完成。
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i=1; i<=6; i++){
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"任务完成..");
countDownLatch.countDown();
},String.valueOf(i)).start();//给线程命名
}
countDownLatch.await();
System.out.println("任务完成");
}
}
结果:
思路就是:把数值减少到0才会放行。