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信息同一秒出现了?