CountDownLatch(Effective Java 69条目并发工具优先于wait和notify)

public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
        final CountDownLatch ready = new CountDownLatch(concurrency);
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch done = new CountDownLatch(concurrency);
        for(int i = 0; i < concurrency; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    ready.countDown();
                    try {
                        start.await();
                        action.run();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    } finally {
                        done.countDown();
                    }
                }
            });
        }
        ready.await();
        long startNanos = System.nanoTime();
        start.countDown();
        done.await();
        return System.nanoTime() - startNanos;
}

  带控制台打印:

public static long timePrintln(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
        final CountDownLatch ready = new CountDownLatch(concurrency);
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch done = new CountDownLatch(concurrency);
        for(int i = 0; i < concurrency; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("ready.countDown==>"+Thread.currentThread().getName());
                    ready.countDown();
                    try {
                        System.out.println("start.await==>"+Thread.currentThread().getName());
                        start.await();
                        action.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    } finally {
                        System.out.println("done.countDown==>"+Thread.currentThread().getName());
                        done.countDown();
                    }
                }
            });
        }
        System.out.println("ready.await");
        ready.await();
        long startNanos = System.nanoTime();
        System.out.println("start.countDown");
        start.countDown();
        System.out.println("done.await");
        done.await();
        System.out.println("done");
        return System.nanoTime() - startNanos;
}

  测试用例:

public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        try {
            long time = timePrintln(executor, 10, new Runnable() {
                @Override
                public void run() {
                    System.out.println("run...");
                }
            });
            System.out.println(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executor.shutdown();
}

  控制台:

ready.countDown==>pool-1-thread-1
start.await==>pool-1-thread-1
ready.await
ready.countDown==>pool-1-thread-2
start.await==>pool-1-thread-2
ready.countDown==>pool-1-thread-4
ready.countDown==>pool-1-thread-3
start.await==>pool-1-thread-3
start.await==>pool-1-thread-4
ready.countDown==>pool-1-thread-5
start.await==>pool-1-thread-5
ready.countDown==>pool-1-thread-6
ready.countDown==>pool-1-thread-7
start.await==>pool-1-thread-7
start.await==>pool-1-thread-6
ready.countDown==>pool-1-thread-8
start.await==>pool-1-thread-8
ready.countDown==>pool-1-thread-9
start.await==>pool-1-thread-9
ready.countDown==>pool-1-thread-10
start.await==>pool-1-thread-10
start.countDown
done.await
run...
done.countDown==>pool-1-thread-1
run...
done.countDown==>pool-1-thread-2
run...
done.countDown==>pool-1-thread-3
run...
done.countDown==>pool-1-thread-4
run...
done.countDown==>pool-1-thread-5
run...
done.countDown==>pool-1-thread-7
run...
run...
run...
done.countDown==>pool-1-thread-10
run...
done.countDown==>pool-1-thread-6
done.countDown==>pool-1-thread-9
done.countDown==>pool-1-thread-8
done
1391850

 

    原文作者:小鼠甲
    原文地址: http://www.cnblogs.com/lmxsj/p/7840104.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞