2018-09-15(JDK并发工具类_CountDownLatch)

CountDownLatch 程序计数器.


代码如下:

ExecutorService es= Executors.newCachedThreadPool();
      // 创建一个CountDownLatch 
        final CountDownLatch countDownLatch=new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount; i++) {

            int finalI = i;
            es.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        es(finalI);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }

        countDownLatch.await();
        log.info("this is finish");
        es.shutdown();

理解:
这是一个计数器,控制多线程并发的.

你可能感兴趣的:(2018-09-15(JDK并发工具类_CountDownLatch))