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

    

啦啦啦

posted @   limeOracle  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2016-11-28 Ubuntu默认防火墙安装、启用、配置、端口、查看状态相关信息
点击右上角即可分享
微信分享提示