int count = 4;
CountDownLatch countDownLatch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
System.out.println(Thread.currentThread().getName() + " 开始时间:" + System.currentTimeMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.setName("线程-" + i);
thread.start();
System.out.println(thread.getName() + " 就绪时间:" + System.currentTimeMillis());
countDownLatch.countDown();
}
CountDownLatch并发执行线程
最新推荐文章于 2023-03-18 22:06:40 发布