java 线程 新类库中的构件 countDownLatch 使用
来源:互联网 发布:2017年9月宏观数据 编辑:程序博客网 时间:2024/05/16 01:52
package org.rui.thread.newc;import java.util.Random;import java.util.concurrent.CountDownLatch;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * Latch 锁存器 * 新类库中的构件 countDownLatch * * @author lenovo * */class TaskPortion implements Runnable {private static int counter = 0;private final int id = counter++;private static Random rand = new Random(47);private final CountDownLatch latch;public TaskPortion(CountDownLatch latch) {this.latch = latch;}@Overridepublic void run() {try {doWork();latch.countDown();} catch (InterruptedException e) {// acceptable way to exit}}// 处理业务代码public void doWork() throws InterruptedException {TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));System.out.println(this + " 完成");}public String toString() {return String.format("%1$-3d", id);}}// waits on the countDownLatchclass WaitingTask implements Runnable {private static int counter = 0;// 计数private final int id = counter++;private static Random rand = new Random(47);private final CountDownLatch latch;WaitingTask(CountDownLatch latch) {this.latch = latch;}@Overridepublic void run() {try {// 调用countDown()的任务在产生调用时并没有被阻塞,只有对await的调用会被阻塞,直至计数值到达0// 等待问题被解决的任务在这个锁存器上调用await(),将它们自已拦住,直至锁存器计数结束latch.await();System.out.println("latch 障碍被认为 " + this);} catch (InterruptedException e) {System.out.println(this + " interrupted");}}public String toString() {return String.format("waitingTask %1$-3d", id);}}/** * TaskPortio将随机地休眠一段时间,以模拟这部分工作的完成,而WaitingTask表示系统中等待的部分,它要等待到问题的初始部分成完为止, * 所有的任务都使用了在main中定义同一个单一的counDownLacth * * @author lenovo * */public class CountDownLatchDemo {static final int SIZE = 100;public static void main(String[] args) throws InterruptedException {ExecutorService exec = Executors.newCachedThreadPool();CountDownLatch latch = new CountDownLatch(SIZE);// 都必须共享一个countDownLatch对象for (int i = 0; i < 10; i++) {exec.execute(new WaitingTask(latch));// 这个要等待 latch上面的为0时才会执行}for (int i = 0; i < SIZE; i++) {exec.execute(new TaskPortion(latch));}// latch.await();System.out.println("launched all tasks");exec.shutdown();// quit when all task complete}}/**output:launched all tasks43 完成95 完成99 完成36 完成94 完成11 完成....12 完成1 完成27 完成98 完成13 完成72 完成71 完成2 完成45 完成92 完成31 完成14 完成17 完成6 完成97 完成....80 完成....56 完成85 完成61 完成30 完成....3 完成93 完成81 完成78 完成73 完成44 完成82 完成49 完成64 完成83 完成16 完成latch 障碍被认为 waitingTask 2 latch 障碍被认为 waitingTask 0 latch 障碍被认为 waitingTask 4 latch 障碍被认为 waitingTask 1 latch 障碍被认为 waitingTask 5 latch 障碍被认为 waitingTask 3 latch 障碍被认为 waitingTask 7 latch 障碍被认为 waitingTask 6 latch 障碍被认为 waitingTask 9 latch 障碍被认为 waitingTask 8 */
1 0
- java 线程 新类库中的构件 countDownLatch 使用
- java 线程 新类库中的构件 CyclicBarrier使用
- java线程中锁存器CountDownLatch的使用
- Java线程池及CountDownLatch的使用
- java线程中锁存器CountDownLatch的使用
- java使用CountDownLatch实现线程顺序执行
- Java线程之CountDownLatch
- java线程之CountDownLatch
- java线程 CountDownLatch
- 线程中的AtomicInteger和CountDownLatch
- java线程池 与 同步计数器CountDownLatch的使用。
- Java并发编程: 使用CountDownLatch协调子线程
- Java线程计数器CyclicBarrier和CountDownLatch的使用
- java中CountDownLatch线程同步的入门使用
- java线程同步辅助类的使用CountDownLatch
- java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例
- Java中的信号量:CountDownLatch
- Java中的计数器CountDownLatch
- 让ROOT权限一次获取,终生受用
- 重做日志的管理
- Java 语言规范
- (MS)Design an algorithm to find the maximum subsquare such that all four borders are filled with bla
- JOAL学习笔记 第二课 循环与淡出
- java 线程 新类库中的构件 countDownLatch 使用
- 虚拟网卡概述
- 史上最全的javascript知识点总结,浅显易懂。
- ubuntu 14.04 安装搜狗拼音输入法
- GestureDetector和SimpleOnGestureListener的使用教程
- Java环境变量的配置
- 星际之门(一)
- SGU 543 Cafe
- 阿里云产品助企业腾飞 —— 来自云上架构实践者们的经验分享