/** * <p><b>sample usage:</b> here is a pair of classes in which a group * of worker threads use two countdown latches: * <ul> * <li>the first is a start signal that prevents any worker from proceeding * until the driver is ready for them to proceed; * <li>the second is a completion signal that allows the driver to wait * until all workers have completed. * </ul> **/
样本用法:这是一对组中的一个类工作线程使用两个倒计时锁存器:
第一个是启动信号,阻止任何工作人员继续进行直到司机准备好继续进行;
第二个是完成信号,允许驾驶员等待直到所有工人完成。
实例代码
public class driver { public static void main(string[] args) throws interruptedexception { countdownlatch startsignal = new countdownlatch(1); countdownlatch donesignal = new countdownlatch(5); for (int i=0;i<5;++i){ new thread(new worker(startsignal, donesignal)).start(); } system.out.println("all thread start up."); // 任务开始信号 startsignal.countdown(); system.out.println(" startsignal count down. "); // 等待所有线程执行完成后才执行后续代码 donesignal.await(); system.out.println(" all thread end up. "); // 执行结果 // all thread start up. // startsignal count down. // hello world! // hello world! // hello world! // hello world! // hello world! // all thread end up. } } class worker implements runnable{ private final countdownlatch startsignal; private final countdownlatch donesignal; worker(countdownlatch startsignal, countdownlatch donesignal) { this.startsignal = startsignal; this.donesignal = donesignal; } @override public void run() { try { // 阻止线程执行,直到startsignal.countdown()后开始执行 startsignal.await(); dowork(); donesignal.countdown(); } catch (interruptedexception e) { e.printstacktrace(); } } void dowork(){ system.out.println("hello world!"); } }
/* * <p>another typical usage would be to divide a problem into n parts, * describe each part with a runnable that executes that portion and * counts down on the latch, and queue all the runnables to an * executor. when all sub-parts are complete, the coordinating thread * will be able to pass through await. (when threads must repeatedly * count down in this way, instead use a {@link cyclicbarrier}.) **/
另一种典型用法是将问题分成n个部分,用执行该部分的runnable
描述每个部分倒计时锁定,并将所有runnables
排队到执行人。
当所有子部件完成时,协调线程将能够通过等待。(当线程必须重复时以这种方式倒数,而不是使用{@link cyclicbarrier}
)
实例代码
public class driver2 { public static void main(string[] args) throws interruptedexception { countdownlatch donesignal = new countdownlatch(5); // 创建线程池 executor executor = executors.newsinglethreadexecutor(); for (int i = 0;i<5;++i){ // create and start threads executor.execute(new workerrunnable(donesignal,i)); } // 等待所有线程任务执行完成 donesignal.await(); system.out.println("executor down."); // 关闭线程池 ((executorservice) executor).shutdown(); // 输出结果 // hello world! : 0 // hello world! : 1 // hello world! : 2 // hello world! : 3 // hello world! : 4 // executor down. } } class workerrunnable implements runnable { private final countdownlatch donesignal; private final int i; workerrunnable(countdownlatch donesignal, int i) { this.donesignal = donesignal; this.i = i; } @override public void run() { dowork(i); donesignal.countdown(); } void dowork(int count) { system.out.println("hello world! : " + count); } }
如对本文有疑问, 点击进行留言回复!!
Android 4.0使用Kotlin调用C语言以及汇编语言
Java Class.forName()用法和newInstance()方法原理解析
网友评论