Android 一个Java的同步工具类 CountDownLatch

http://www.importnew.com/15731.html

这个是文档...

俺也说不清  就是线程运行可控

 CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(5);

        // 依次创建并启动5个worker线程
        for (int i = 0; i < 5; ++i) {
            new Thread(new Worker(startSignal, doneSignal)).start();
        }

        System.out.println("Driver is doing something...");
        System.out.println("Driver is Finished, start all workers ...");
        startSignal.countDown(); // Driver执行完毕,发出开始信号,使所有的worker线程开始执行
        try {
            doneSignal.await(); // 等待所有的worker线程执行结束
            System.out.println("Finished.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
  class Worker implements Runnable {
        private final CountDownLatch startSignal;
        private final CountDownLatch doneSignal;

        Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
            this.startSignal = startSignal;
            this.doneSignal = doneSignal;
        }

        public void run() {
            try {
                startSignal.await(); // 等待Driver线程执行完毕,获得开始信号
                System.out.println("Working now ...");
                doneSignal.countDown(); // 当前worker执行完毕,释放一个完成信号
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/FlyPig_Vip/article/details/88530996