标签:经典 string span nbsp eth starting 个数 when start
public CountDownLatch(int count)
public class CountDownLatchDemo implements Runnable { static final CountDownLatch end = new CountDownLatch(10); static final CountDownLatchDemo demo = new CountDownLatchDemo(); /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object‘s * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { try { Thread.sleep(new Random().nextInt(10) * 1000); System.out.println("check complete"); end.countDown();//完成 可以减1 } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { ExecutorService exec = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { exec.submit(demo); } end.await();//主线程等待所有10个线程完成任务 才能继续执行 System.out.println("Fire!"); exec.shutdown(); } }
倒计时:CountDownLatch(火箭发射前的准备)读书笔记
标签:经典 string span nbsp eth starting 个数 when start
原文地址:http://www.cnblogs.com/ten951/p/6212147.html