一、案例代码
//计数器
CountDownLatch countDownLatch = new CountDownLatch(2);
//执行线程1
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ",子行程开始执行。。。");
//计数器减1
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName() + ",子行程执行结束。。。");
}).start();
//执行线程1
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ",子行程开始执行。。。");
//计数器减1
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName() + ",子行程执行结束。。。");
}).start();
//减去为0,恢复任务继续执行
countDownLatch.await();
System.out.println("两个子线程执行完毕....");
System.out.println("主线程继续执行.....");
for (int i = 0; i <10; i++) {
System.out.println("main,i:"+i);
}