CountDownLatch 多功能同步工具
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * CountDownLatch * 同步策略 允许一个或多个线程等待一些列其它线程操作完成后执行 * 由给定count初始化 * await方法等待所有等待线程countDown方法执行之后释放 * count不能重置,只能使用一次 对比CyclicBarrier * 多用途同步工具: * 初始化为1的CountDownLatch可以作为 on/off开关;所有线程等待直到所有线程都执行了countDown gate效果 * 初始化为N.... * * 必须等待所有线程都执行完成后才能让所有线程继续执行时其比较有用的特性 * * Created by windwant on 2016/5/27. */ public class MyCountDownLatch { public static void main(String[] args) throws InterruptedException { ExecutorService es = Executors.newCachedThreadPool(); CountDownLatch cd = new CountDownLatch(5); Random r = new Random(); es.execute(new SubWork(cd, r.nextInt(10), "task1")); es.execute(new SubWork(cd, r.nextInt(10), "task2")); es.execute(new SubWork(cd, r.nextInt(10), "task3")); es.execute(new SubWork(cd, r.nextInt(10), "task4")); es.execute(new SubWork(cd, r.nextInt(10), "task5")); cd.await(); es.execute(new MainWork()); es.shutdown(); } } class MainWork implements Runnable { public void run() { try { System.out.println("mian task begin"); for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("============" + i + "============"); } System.out.println("mian task implemented"); } catch (Exception e) { e.printStackTrace(); } } } class SubWork implements Runnable{ private CountDownLatch cd; private int seconds; private String taskName; SubWork(CountDownLatch cd, int seconds, String taskName){ this.cd = cd; this.seconds = seconds; this.taskName = taskName; } public void run() { try{ System.out.println("subwork " + taskName + " begin, need time: " + seconds + "s"); long b = System.currentTimeMillis(); for (int i = 0; i < seconds; i++) { Thread.sleep(1000); System.out.println("subtask: " + taskName + "============" + i + "============"); } long d = System.currentTimeMillis() - b; System.out.println("subwork " + taskName + " over, executing time: " + TimeUnit.SECONDS.convert(d, TimeUnit.MILLISECONDS)); cd.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }
项目地址:https://github.com/windwant/threadtest
相关推荐
ErixHao 2020-05-30
ALiDan 2020-04-16
王道革 2020-04-11
sunzxh 2020-03-23
付春杰Blog 2020-02-17
spb 2020-02-01
xzkjgw 2020-01-17
abdstime 2020-01-17
Cheetahcubs 2020-01-04
zhujiangtaotaise 2019-12-26
zhangxiaocc 2019-11-07
Wisher 2017-06-07
思捻如枫 2019-07-01
leys 2019-07-01
讨厌什么变成什么 2019-06-30
浪里xiao白龙 2019-06-28
mojianc 2019-06-28
liwenbocsu 2019-06-28
了缺 2019-06-28