ITPub博客

首页 > 应用开发 > > 关于下面CountDownLatch代码段的疑问

关于下面CountDownLatch代码段的疑问

作者:choubou 时间:2019-08-21 10:27:58 0 删除 编辑
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/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: VC++截取的方法
请登录后发表评论 登录
全部评论

注册时间:2019-10-08

  • 博文量
    175
  • 访问量
    10707