倒计时器CountDownLatch使用个例
1public class Test { 2 static final CountDownLatch end = new CountDownLatch(10); 3 4 static class CounDownLatchDemo implements Runnable { 5 6 @Override 7 public void run() { 8 try { 9 Thread.sleep(1000); 10 System.out.println("check complete"); 11 end.countDown(); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 17 } 18 19 public static void main(String[] args) { 20 CounDownLatchDemo demo = new CounDownLatchDemo(); 21 ExecutorService exs = Executors.newFixedThreadPool(10); 22 for (int i = 0; i < 10; i++) { 23 exs.submit(demo); 24 } 25 try { 26 // 等待所有任务结束完毕,再继续执行主线程 27 end.await(); 28 } catch (InterruptedException e) { 29 e.printStackTrace(); 30 } 31 System.out.println("Fire!"); 32 exs.shutdown(); 33 } 34 35}