程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> CountDownLatch 多功能同步工具,countdownlatch

CountDownLatch 多功能同步工具,countdownlatch

編輯:JAVA綜合教程

CountDownLatch 多功能同步工具,countdownlatch


 1 package com.thread.test.thread;
 2 
 3 import java.util.Random;
 4 import java.util.concurrent.*;
 5 
 6 /**
 7  * CountDownLatch
 8  * 同步策略 允許一個或多個線程等待一些列其它線程操作完成後執行
 9  * 由給定count初始化
10  * await方法等待所有等待線程countDown方法執行之後釋放
11  * count不能重置,只能使用一次 對比CyclicBarrier
12  * 多用途同步工具:
13  * 初始化為1的CountDownLatch可以作為 on/off開關;所有線程等待直到所有線程都執行了countDown gate效果
14  * 初始化為N....
15  *
16  * 必須等待所有線程都執行完成後才能讓所有線程繼續執行時其比較有用的特性
17  *
18  * Created by windwant on 2016/5/27.
19  */
20 public class MyCountDownLatch {
21 
22     public static void main(String[] args) throws InterruptedException {
23         ExecutorService es = Executors.newCachedThreadPool();
24         CountDownLatch cd = new CountDownLatch(5);
25         Random r = new Random();
26         es.execute(new SubWork(cd, r.nextInt(10), "task1"));
27         es.execute(new SubWork(cd, r.nextInt(10), "task2"));
28         es.execute(new SubWork(cd, r.nextInt(10), "task3"));
29         es.execute(new SubWork(cd, r.nextInt(10), "task4"));
30         es.execute(new SubWork(cd, r.nextInt(10), "task5"));
31         cd.await();
32         es.execute(new MainWork());
33         es.shutdown();
34     }
35 
36 
37 }
38 
39 class MainWork implements Runnable {
40 
41     public void run() {
42         try {
43             System.out.println("mian task begin");
44             for (int i = 0; i < 5; i++) {
45                 Thread.sleep(1000);
46                 System.out.println("============" + i + "============");
47             }
48             System.out.println("mian task implemented");
49         } catch (Exception e) {
50             e.printStackTrace();
51         }
52     }
53 }
54 
55 class SubWork implements Runnable{
56 
57     private CountDownLatch cd;
58 
59     private int seconds;
60 
61     private String taskName;
62 
63     SubWork(CountDownLatch cd, int seconds, String taskName){
64         this.cd = cd;
65         this.seconds = seconds;
66         this.taskName = taskName;
67     }
68 
69     public void run() {
70         try{
71             System.out.println("subwork " + taskName + " begin, need time: " + seconds + "s");
72             long b = System.currentTimeMillis();
73             for (int i = 0; i < seconds; i++) {
74                 Thread.sleep(1000);
75                 System.out.println("subtask: " + taskName + "============" + i + "============");
76             }
77             long d = System.currentTimeMillis() - b;
78             System.out.println("subwork " + taskName + " over, executing time: " + TimeUnit.SECONDS.convert(d, TimeUnit.MILLISECONDS));
79             cd.countDown();
80         } catch (InterruptedException e) {
81             e.printStackTrace();
82         }
83     }
84 }

項目地址:https://github.com/windwant/threadtest

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved