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 @ 2017-11-28 21:20  limeOracle  阅读(161)  评论(0编辑  收藏  举报