Java并发工具类CountDownLatch源码中的例子
程序员文章站
2022-12-30 08:21:59
Java并发工具类CountDownLatch源码中的例子 实例一 原文描述 样本用法:这是一对组中的一个类工作线程使用两个倒计时锁存器: 第一个是启动信号,阻止任何工作人员继续进行直到司机准备好继续进行; 第二个是完成信号,允许驾驶员等待直到所有工人完成。 实例代码 另一种典型用法是将问题分成N个 ......
java并发工具类countdownlatch源码中的例子
实例一
原文描述
/** * <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开发中总结的Adapter工具类【附完整源码下载】
-
Java日期时间API系列5-----Jdk7及以前的日期时间类TimeUnit在并发编程中的应用
-
Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
-
Java并发工具类CountDownLatch源码中的例子
-
java中金额元转万元工具类的实例
-
根据java 中的例子联想到的constructor ,父类,子类
-
JAVA并发编程(三):同步的辅助类之闭锁(CountDownLatch)与循环屏障(CyclicBarrier)
-
JUC 中的多线程协作工具类:CountDownLatch 和 CyclicBarrier
-
Java中AES加密,解密的工具类,以及MD5加密
-
Java并发编程中CountDownLatch和CyclicBarrier的使用