TIJ -- CountDownLatch
1.
2. Class : CountDownLatchDemo
package lime.thinkingInJava._021._007._001; import java.sql.Time;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition; /**
* @Author : Lime
* @Description :
* @Remark :
*/
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;
}
public 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 + " completed");
} @Override
public String toString() {
return String.format("%1$-3d",id);
}
public static int getCounter(){
return counter;
}
}
//Waits on the CountDownLatch
class WaitingTask implements Runnable{
private static int counter = 0;
private final int id = counter++;
private final CountDownLatch latch;
WaitingTask(CountDownLatch latch){
this.latch = latch;
}
public void run(){
try{
latch.await();
System.out.println("Latch barrier passed for " + this);
}catch (InterruptedException e){
System.out.println(this + " interrupted");
}
}
public String toString(){
return String.format("WaitingTask %1$-3d",id);
}
}
public class CountDownLatchDemo {
static final int SIZE = 100;
public static void main(String[] args) throws InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
//All must share a single CountDownLatch object;
CountDownLatch latch = new CountDownLatch(SIZE);
for(int i = 0;i < 10;i++){
exec.execute(new WaitingTask(latch));
}
for(int i = 0;i < SIZE;i++){
exec.execute(new TaskPortion(latch));
}
System.out.println("Launched all tasks");
TimeUnit.SECONDS.sleep(5);
exec.shutdownNow();//Quit when all tasks complete
System.out.println(TaskPortion.getCounter());
}
}
3. Console :
Launched all tasks
99 completed
43 completed
36 completed
95 completed
94 completed
11 completed
21 completed
77 completed
7 completed
9 completed
75 completed
79 completed
10 completed
40 completed
96 completed
63 completed
23 completed
34 completed
29 completed
38 completed
55 completed
90 completed
88 completed
28 completed
5 completed
50 completed
8 completed
12 completed
1 completed
27 completed
98 completed
13 completed
72 completed
71 completed
2 completed
45 completed
91 completed
31 completed
14 completed
17 completed
6 completed
97 completed
35 completed
69 completed
4 completed
68 completed
42 completed
84 completed
66 completed
70 completed
87 completed
47 completed
46 completed
32 completed
37 completed
86 completed
54 completed
41 completed
20 completed
74 completed
57 completed
65 completed
80 completed
0 completed
19 completed
60 completed
15 completed
89 completed
51 completed
25 completed
53 completed
62 completed
58 completed
92 completed
76 completed
22 completed
56 completed
18 completed
85 completed
61 completed
30 completed
59 completed
67 completed
24 completed
26 completed
48 completed
39 completed
33 completed
52 completed
3 completed
93 completed
81 completed
78 completed
73 completed
44 completed
82 completed
49 completed
64 completed
83 completed
16 completed
Latch barrier passed for WaitingTask 3
Latch barrier passed for WaitingTask 5
Latch barrier passed for WaitingTask 6
Latch barrier passed for WaitingTask 0
Latch barrier passed for WaitingTask 1
Latch barrier passed for WaitingTask 4
Latch barrier passed for WaitingTask 9
Latch barrier passed for WaitingTask 8
Latch barrier passed for WaitingTask 2
Latch barrier passed for WaitingTask 7
4. 鸣谢:
啦啦啦
TIJ -- CountDownLatch的更多相关文章
- 多线程条件通行工具——CountDownLatch
CountDownLatch的作用是,线程进入等待后,需要计数器达到0才能通行. CountDownLatch(int)构造方法,指定初始计数. await()等待计数减至0. await(long, ...
- 【Java并发编程实战】-----“J.U.C”:CountDownlatch
上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...
- 同步辅助类CountDownLatch用法
CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...
- 架构师养成记--12.Concurrent工具类CyclicBarrier和CountDownLatch
java.util.concurrent.CyclicBarrier 一组线程共同等待,直到达到一个公共屏障点. 举个栗子,百米赛跑中,所有运动员都要等其他运动员都准备好后才能一起跑(假如没有发令员) ...
- Java并发之CountDownLatch
CountDownLatch是Java concurrent包下的一个同步工具.它可以让一个(或多个)线程等待,直到其他线程中的某些操作完成. 本质上是一个信号量,我们把它比作一个有N个插销的大门,它 ...
- Java多线程之CountDownLatch学习
给出官网上的例子:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html Java中conc ...
- 【Java】JDK类 CountDownLatch
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达 ...
- 【JUC】JDK1.8源码分析之CountDownLatch(五)
一.前言 分析完了CyclicBarrier后,下面分析CountDownLatch,CountDownLatch用于同步一个或多个任务,强制他们等待由其他任务执行的一组操作完成.CountDownL ...
- java多线程等待协调工作:CountDownLatch类的高级应用
一:说明 基本上对于线程初步了解的人,都是使用synchronized来同步线程的,也确实,它也是可以满足一些常用的问题.那么我们来说一些它不能解决的问题(其实是不怎么好解决的问题,并不是真的不能解决 ...
随机推荐
- Html图像标签、绝对路径和相对路径:
Html图像标签: <img>标签可以在网页上插入一张图片,它是独立使用的标签,它的常用属性有: (1)src 属性 定义图片的引用地址 (2)alt 属性 定义图片加载失败时显示的文字, ...
- 向excel中循环插入值
import xlrd #导入excel读模块 from xlutils import copy #导入copy模块 book = xlrd.open_workbook('tb_base_buildi ...
- JSAP102
JSAP102 1.API //案例:禁用文本框 <body> <input type="button" value="禁用" id=&quo ...
- java C 类自动转换规则
C类型转换规则
- delphi 响应鼠标进入控件消息
procedure MSGEnter(var msg:TMessage);message CM_MOUSEENTER;//响应进入的消息 procedure MSGLeave(var msg: TMe ...
- 微信公众号申请+新浪SAE申请
一. 新浪SAE服务申请 1. 注冊地址:http://t.cn/RqMHPto 2. 选择控制台>>云应用SAE 3. 创建新应用 4. 填写域名 5. 代码管理选择SVN 6. 创建版 ...
- Go 语言极速入门
本系列文章主要是记录<Go 语言实战>和<Google 资深工程师深度讲解 Go 语言>的学习笔记. Go 语言极速入门1 - 环境搭建与最简姿势Go 语言极速入门2 - 基础 ...
- C# ReaderWriterLockSlim 实现
其实ReaderWriterLockSlim的实现前段时间看了,当时不打算记录下来的,因为它的实现实在System.Core项目里面,而不是mscorlib项目.按照惯例我们还是先看看网上的一些说法吧 ...
- dotnet ef
dotnet ef migrations add <Name-of-Migration> dotnet ef database update
- 阿里云服务器CentOS7 vsftp安装、设置及后台端口的设置
查看是否安装vsftp,我这个是已经安装的. [root@localhost vsftpd]# rpm -qa |grep vsftpd vsftpd-3.0.2-11.el7_2.x86_64 如果 ...