首页 > 应用开发 > > 关于下面CountDownLatch代码段的疑问
public class CountDownLatchExample1 {
private static int threadCount = 10;
public static void main(String[] args) throws InterruptedException {
ExecutorService es = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
final int count = i;
es.execute(() -> {
try {
test(count);
} catch (Exception e) {
log.error("Exception,{}", e);
} finally {
countDownLatch.countDown();
}
});
}
countDownLatch.await(1, TimeUnit.MILLISECONDS);
log.info("======");
es.shutdown();
}
public static void test(int i) throws InterruptedException {
Thread.sleep(1000);//此方法延迟1秒再执行
log.info("test {}", i);
}
}
由于在方法里面加了一秒延迟 所以先打印出======,那为什么其他10条log信息同一秒出现了?
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4548/viewspace-3133774/ ,如需转载,请注明出处,否则将追究法律责任。