java线程--倒计时器CountDownLatch

倒计时器CountDownLatch使用个例

public class Test {
    static final CountDownLatch end = new CountDownLatch(10);

    static class CounDownLatchDemo implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("check complete");
                end.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        CounDownLatchDemo demo = new CounDownLatchDemo();
        ExecutorService exs = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            exs.submit(demo);
        }
        try {
            // 等待全部任务结束完毕,再继续执行主线程
            end.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Fire!");
        exs.shutdown();
    }

}
相关文章
相关标签/搜索