package com.zxl.chapter54;
import java.util.concurrent.CountDownLatch;
public class Test {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(10);
// 启动线程
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
// 执行线程任务
System.out.println("Thread completed"+ Thread.currentThread().getName());
} finally {
latch.countDown(); // 任务完成后调用 countDown
}
}).start();
}
// 等待所有线程完成
latch.await();
}
}