首页 技术 正文
技术 2022年11月17日
0 收藏 932 点赞 3,496 浏览 3328 个字

  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. 鸣谢:

    

啦啦啦

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,846