main thread do something-1 pool-1-thread-1 do something! pool-1-thread-2 do something! pool-1-thread-3 do something! pool-1-thread-5 do something! pool-1-thread-6 do something! pool-1-thread-7 do something! pool-1-thread-8 do something! pool-1-thread-4 do something! pool-1-thread-9 do something! pool-1-thread-10 do something! countDownLatch: java.util.concurrent.CountDownLatch@76fb509a[Count = 0] main thread do something-2
CountDownLatch常用方法
public void await() throws InterruptedException:调用await()方法的线程会被挂起,等待直到count值为0再继续执行。
public boolean await(long timeout, TimeUnit unit) throws InterruptedException:同await(),若等待timeout时长后,count值还是没有变为0,不再等待,继续执行。时间单位如下常用的毫秒、天、小时、微秒、分钟、纳秒、秒。
public void countDown(): count值递减1.
public long getCount():获取当前count值。
public String toString():重写了toString()方法,多打印了count值,具体参考源码。
/** * Constructs a {@code CountDownLatch} initialized with the given count. * * @param count the number of times {@link #countDown} must be invoked * before threads can pass through {@link #await} * @throws IllegalArgumentException if {@code count} is negative */ public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); }
toString()方法源码
1 2 3 4 5 6 7 8 9 10
/** * Returns a string identifying this latch, as well as its state. * The state, in brackets, includes the String {@code "Count ="} * followed by the current count. * * @return a string identifying this latch, as well as its state */ public String toString() { return super.toString() + "[Count = " + sync.getCount() + "]"; }
/** * a start signal that prevents any worker from proceeding * until the driver is ready for them to proceed; * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(10); for (int i = 0; i < 10; i++) { // create and start threads new Thread(new Worker(startSignal, doneSignal)).start(); } // don't let run yet System.out.println("do something else 1"); // let all threads proceed startSignal.countDown(); System.out.println("do something else 2"); // wait for all to finish doneSignal.await(); System.out.println("wait for all to finsh"); }
static class Worker implements Runnable{
private final CountDownLatch startSignal; private final CountDownLatch doneSignal;
do something else 1 do something else 2 do work! do work! do work! do work! do work! do work! do work! do work! do work! do work! wait for all to finsh
/** * a completion signal that allows the driver to wait * until all workers have completed. * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { CountDownLatch doneSignal = new CountDownLatch(5); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { // create and start threads cachedThreadPool.execute(new Worker(doneSignal, i)); } // don't let run yet System.out.println("do something else 1"); // wait for all to finish doneSignal.await(); System.out.println("===========================count: " + doneSignal.getCount()); System.out.println("do something else 2"); cachedThreadPool.shutdown(); }
static class Worker implements Runnable{
private final CountDownLatch doneSignal; private final int i;
do something else 1 do work! i = 0, java.util.concurrent.CountDownLatch@128abd43[Count = 4] do work! i = 1, java.util.concurrent.CountDownLatch@128abd43[Count = 3] do work! i = 2, java.util.concurrent.CountDownLatch@128abd43[Count = 2] do work! i = 3, java.util.concurrent.CountDownLatch@128abd43[Count = 1] do work! i = 4, java.util.concurrent.CountDownLatch@128abd43[Count = 0] ===========================count: 0 do something else 2 do work! i = 5, java.util.concurrent.CountDownLatch@128abd43[Count = 0] do work! i = 6, java.util.concurrent.CountDownLatch@128abd43[Count = 0] do work! i = 7, java.util.concurrent.CountDownLatch@128abd43[Count = 0] do work! i = 8, java.util.concurrent.CountDownLatch@128abd43[Count = 0] do work! i = 9, java.util.concurrent.CountDownLatch@128abd43[Count = 0]