public class CountDownLatchDemo {
public static void main(String[] args) {
try {
CountDownLatch downLatch = new CountDownLatch(3);
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "ready");
// *********将CountDownLatch的数值减1
downLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
System.out.println("=== begin lunch ===");
// "主线程等待线程池中3个任务的完成
downLatch.await();
System.out.println("=== end lunch ===");
} catch (Exception e) {
}
}
}
Java 7之多线程- CountDownLatch
最新推荐文章于 2023-05-12 01:00:00 发布