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的更多相关文章

  1. 多线程条件通行工具——CountDownLatch

    CountDownLatch的作用是,线程进入等待后,需要计数器达到0才能通行. CountDownLatch(int)构造方法,指定初始计数. await()等待计数减至0. await(long, ...

  2. 【Java并发编程实战】-----“J.U.C”:CountDownlatch

    上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...

  3. 同步辅助类CountDownLatch用法

    CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...

  4. 架构师养成记--12.Concurrent工具类CyclicBarrier和CountDownLatch

    java.util.concurrent.CyclicBarrier 一组线程共同等待,直到达到一个公共屏障点. 举个栗子,百米赛跑中,所有运动员都要等其他运动员都准备好后才能一起跑(假如没有发令员) ...

  5. Java并发之CountDownLatch

    CountDownLatch是Java concurrent包下的一个同步工具.它可以让一个(或多个)线程等待,直到其他线程中的某些操作完成. 本质上是一个信号量,我们把它比作一个有N个插销的大门,它 ...

  6. Java多线程之CountDownLatch学习

    给出官网上的例子:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html Java中conc ...

  7. 【Java】JDK类 CountDownLatch

    一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达 ...

  8. 【JUC】JDK1.8源码分析之CountDownLatch(五)

    一.前言 分析完了CyclicBarrier后,下面分析CountDownLatch,CountDownLatch用于同步一个或多个任务,强制他们等待由其他任务执行的一组操作完成.CountDownL ...

  9. java多线程等待协调工作:CountDownLatch类的高级应用

    一:说明 基本上对于线程初步了解的人,都是使用synchronized来同步线程的,也确实,它也是可以满足一些常用的问题.那么我们来说一些它不能解决的问题(其实是不怎么好解决的问题,并不是真的不能解决 ...

随机推荐

  1. python字符串面试题:找出一个字符串中第一个字母和最后一个字符是第一次重复,中间没有重复且最长的子串

    1.给出任意一个字符串,打印一个最长子串字符串及其长度,如果有相同长度的子字符串,都要一起打印出来,该子字符串满足以下条件, 第一个字母和最后一个字符是第一次重复 这个子字符串的中间字母没有重复 这个 ...

  2. python之编程风格

    第一:语句和语法 #   表示注释掉的内容 \    续行 print("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\ yyyyyyyyyyyyyyyyyyyyy ...

  3. 使用linux kernel代码编译perf工具

    环境:Qemu + ARMv8 perf是一款综合性分析工具,大到系统全局性性能,再小到进程线程级别,甚至到函数及汇编级别. 在内核源码目录下执行编译脚本: #!/bin/bash cross_com ...

  4. 通过脚本调用MSBuild编译项目时指定Configuration(解決方案配置)和Platform(解決方案平台),Rebuid(重新生成解决方案),Clean(清理解决方案)

    为了方便打包测试,自己PowerShell写了一个编译和发布的脚本,调用msbuild通过命令行来编译当前解决方案 后来发现一个问题,用VS编译解决方案,我通过 项目属性-Build设置 Releas ...

  5. android: 在APP中显示高德地图SDK

    一.搭建环境 参考资料:http://lbs.amap.com/api/android-sdk/guide/create-project/android-studio-create-project  ...

  6. tp5.1的安装与运行流程

    今天安装tp5.1的时候出现问题,我这里使用的是将tp直接解压缩后放在项目里面,结果提示下面的错误:Warning: require(D:\workspace\zhiyuedu\vendor/topt ...

  7. Centos如何安装163yum源

    如果Centos使用系统自带的yum源,在用yum进行安装的时候,速度会受到限制,所以我们需要替换为国内的yum源,一般会选择163源,速度比较快包也比较全,使用yum进行安装的时候可以大大节省时间 ...

  8. 【ASP.NET Core】EF Core - “导航属性”

    “导航属性”是实体框架用得算是比较频繁的概念. 首先,它是类型成员,其次,他是属性,这不是 F 话,而是明确它的本质.那么,什么场景下会用到导航属性呢?重点就落在“导航”一词上了,当实体 A 需要引用 ...

  9. ubuntu 16.04 源代码安装libusb

    libusb是一个跨平台的usb通讯库:https://libusb.info/ 在ubuntu16上安装 1. 首先安装 libudev-dev sudo apt-get install libud ...

  10. JAVA使用Logback发送日志到控制台、文件、ELK的最简单用法

    一.简述 本文讲JAVA使用Logback发送日志到控制台.文件.ELK的最简单用法. 二.教程 1.新建pom.xml项目引入下列依赖: <dependencies> <!--Be ...