一、CountDownLatchDemo

package com.duchong.concurrent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch; /*
* CountDownLatch :阻塞主线程,等子线程完成
*/
public class CountDownLatchDemo { /**
* 存放子线程产生的结果
*/
private static ConcurrentHashMap<String,Integer> resultMap =new ConcurrentHashMap<>(); public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(5); SubThread subThread = new SubThread(latch); for (int i = 0; i <=4; i++) {
new Thread(subThread).start();
}
try {
//阻塞主线程
latch.await();
}
catch (InterruptedException e) {
} //计算总结果
int sum=0;
for(Map.Entry<String,Integer> subNumber:resultMap.entrySet()){
sum +=subNumber.getValue();
}
System.out.println("sum = "+sum);
} /**
* 子线程
*/
static class SubThread implements Runnable { private CountDownLatch latch; public SubThread(CountDownLatch latch) {
this.latch = latch;
} @Override
public void run() { String name = Thread.currentThread().getName();
try {
int number = RandomUtil.getNumber();
System.out.println(name+"---number:"+number);
resultMap.put(name,number);
}
finally {
latch.countDown();
} } }
}

二、CyclicBarrierDemo

package com.duchong.concurrent;

import java.util.Map;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CyclicBarrier; /*
* CyclicBarrier :阻塞子线程,当等待中的子线程数到达一定数量时,跳闸。
*/
public class CyclicBarrierDemo { /**
* 屏障,初始5 当await()的线程数量达到5时,跳闸。
*/
static CyclicBarrier c = new CyclicBarrier(5,new SumThread()); /**
* 存放子线程产生的结果
*/
private static ConcurrentHashMap<String,Integer> resultMap =new ConcurrentHashMap<>(); public static void main(String[] args) { //模拟四个子线产生随机数值
for(int i=0;i<=4;i++){
new Thread(new SubThread()).start();
}
} /**
* 所有子线程等待数等于5时,执行
*/
private static class SumThread implements Runnable{ @Override
public void run() {
int result =0;
for(Map.Entry<String,Integer> workResult:resultMap.entrySet()){
result = result+workResult.getValue();
}
System.out.println("result = "+result);
}
} /**
* 子线程
*/
static class SubThread implements Runnable{ @Override
public void run() { String name = Thread.currentThread().getName(); int number = RandomUtil.getNumber();
System.out.println(name+"---number:"+number); resultMap.put(name,number); try {
//阻塞子线程
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
} }
} }

CountDownLatch和CyclicBarrier使用上的区别的更多相关文章

  1. CountDownLatch、CyclicBarrier、Semaphore的区别

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就学习一下这三个辅助类的用法. 以下是 ...

  2. Java的CountDownLatch和CyclicBarrier的理解和区别

    CountDownLatch和CyclicBarrier的功能看起来很相似,不易区分,有一种谜之的神秘.本文将通过通俗的例子并结合代码讲解两者的使用方法和区别. CountDownLatch和Cycl ...

  3. CountDownLatch与CyclicBarrier的使用与区别

    CountDownLatch的介绍和使用: 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 co ...

  4. java 并发包JUC下的CountDownLatch和CyclicBarrier的理解和区别

    推荐这篇帖子,讲得不错~ https://blog.csdn.net/liangyihuai/article/details/83106584

  5. java高并发之CountDownLatch,CyclicBarrier和join

    晚上打车回家,在车上看到一篇文章<22岁大学生获谷歌天价Offer,年薪千万!>,讲的是印度一个22岁大学生多次参加ACM大赛,开源多个项目,以非常牛逼的履历通过了谷歌的AI测试,斩获谷歌 ...

  6. CountDownLatch和CyclicBarrier的区别

    [CountDownLatch.CyclicBarrier和Semaphore]http://www.cnblogs.com/dolphin0520/p/3920397.html   [CountDo ...

  7. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  8. JUC之CountDownLatch和CyclicBarrier的区别 (转)

    CountDownLatch和CyclicBarrier的功能看起来很相似,不易区分,有一种谜之的神秘.本文将通过通俗的例子并结合代码讲解两者的使用方法和区别. CountDownLatch和Cycl ...

  9. CountDownLatch和CyclicBarrier的区别(转)

    在网上看到很多人对于CountDownLatch和CyclicBarrier的区别简单理解为CountDownLatch是一次性的,而CyclicBarrier在调用reset之后还可以继续使用.那如 ...

随机推荐

  1. noi.ac #37 dp计数

    #include<algorithm> #include<cstring> #include<cstdio> #include<iostream> ty ...

  2. mpvue搭建小程序框架

    http://mpvue.com/mpvue/ 美团开源了mpvue 由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler) 运行时框架 runtime 和代码编译器 c ...

  3. Strategic game(树形DP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:一棵树,要放置哨兵,要求最少放置多少哨兵能监视到所有的结点 题目分析: 放置哨兵无非两 ...

  4. C#/Java 常用轮子 (子文章)(持续更新)

    -----> 总文章 入口 C# 框架/类库名称 介绍 Topshelf windows服务框架 Quartz 定时任务框架 NVelocity MVC视图引擎 NPOI 文档读写 Signal ...

  5. jquery中清空

    jQuery("#map_bmmc").empty();   function qingkong(){         $("#form1").find(&qu ...

  6. spaceclaim脚本(内摆线)

    import math #导入数学模块,因为会使用π def x_comp(k,r,t): #定义x坐标的计算函数 return r * (k -1) * math.cos(t) + r * math ...

  7. receipt

    receipt - 必应词典 美[riˈsiːt]英[rɪ'siːt] n.收据:收入:接受:字据 v.开收据 网络收到:收条:发票 变形复数:receipts: 搭配give receipt:sig ...

  8. js实现replaceAll方法

    js本来有replace方法,请看w3school的说明: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法: stringObject.rep ...

  9. [Java/File]读取日文CSV文件不乱码

    try { StringBuilder sb=new StringBuilder(); sb.append("\nContent in File:'"+filePathname+& ...

  10. pip install失败报错解决方案

    cmd pip install 某些包时报错 pip install Consider using the `--user` option or check the permissions. 只需要p ...