1、join
模拟现在有三个worker,采用join的方法控制:

package org.pbccrc.org.pbccrc.thread;public class Test {public static void main(String[] args) throws InterruptedException {Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000));Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000));Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000));Thread thread1 =new Thread(worker1);Thread thread2 =new Thread(worker2);Thread thread3 =new Thread(worker3);thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("wait thread1 and thread2 complete :");thread3.start();thread3.join();System.out.println("three threads have complete!");}}
package org.pbccrc.org.pbccrc.thread;public class Worker implements Runnable {private String name;private int time;public Worker(String name, int time) {super();this.name = name;this.time = time;}@Overridepublic void run() {System.out.println("task :"+name+":is running!");try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("task :"+name+":complete!");}}
task :第一个任务:is running!
task :第二个任务:is running!
task :第二个任务:complete!
task :第一个任务:complete!
wait thread1 and thread2 complete :
task :第三个任务:is running!
task :第三个任务:complete!
three threads have complete!

2Countdownlatch
采用countDownlatch控制:

package org.pbccrc.org.pbccrc.thread;import java.util.concurrent.CountDownLatch;public class Test {public static void main(String[] args) throws InterruptedException {CountDownLatch latch=new CountDownLatch(2);Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000),latch);Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000),latch);Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000),latch);Thread thread1 =new Thread(worker1);Thread thread2 =new Thread(worker2);Thread thread3 =new Thread(worker3);thread1.start();thread2.start();latch.await();System.out.println("wait thread1 and thread2 complete :");thread3.start();thread3.join();System.out.println("three threads have complete!");}}
package org.pbccrc.org.pbccrc.thread;import java.util.concurrent.CountDownLatch;public class Worker implements Runnable {private String name;private int time;private CountDownLatch latch;public Worker(String name, int time,CountDownLatch latch) {super();this.name = name;this.time = time;this.latch = latch;}@Overridepublic void run() {System.out.println("task :"+name+":is running!");try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("task :"+name+":complete!");latch.countDown();    }}
task :第二个任务:is running!
task :第一个任务:is running!
task :第二个任务:complete!
task :第一个任务:complete!
wait thread1 and thread2 complete :
task :第三个任务:is running!
task :第三个任务:complete!
three threads have complete!

我们发现好像join与countdownlatch没有区别:看下面的例子吧:

3、分阶段控制使用countdownlatch

package org.pbccrc.org.pbccrc.thread;import java.util.concurrent.CountDownLatch;public class Worker implements Runnable {private String name;private int time;private CountDownLatch latch;public Worker(String name, int time,CountDownLatch latch) {super();this.name = name;this.time = time;this.latch = latch;}@Overridepublic void run() {System.out.println("task :"+name+":is running for first process!");try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}latch.countDown();   try {System.out.println("task :"+name+":is begin for second process!");Thread.sleep(time);System.out.println("task :"+name+":is complete for second process!");} catch (InterruptedException e) {e.printStackTrace();}System.out.println("task :"+name+":complete!");}}
task :第一个任务:is running for first process!
task :第二个任务:is running for first process!
task :第一个任务:is begin for second process!
task :第二个任务:is begin for second process!
wait thread1 and thread2 complete :
task :第三个任务:is running for first process!
task :第一个任务:is complete for second process!
task :第一个任务:complete!
task :第二个任务:is complete for second process!
task :第二个任务:complete!
task :第三个任务:is begin for second process!
task :第三个任务:is complete for second process!
task :第三个任务:complete!
three threads have complete!

区别在:线程执行过程中我们可以使用countdownlatch的方法countDown;countdownlatch是减数器,当数量为0时会唤醒其他的线程执行;

4、CyclicBarrier

public class Test {public static void main(String[] args) throws InterruptedException, BrokenBarrierException {CyclicBarrier barrier=new CyclicBarrier(3);Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000),barrier);Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000),barrier);Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000),barrier);Thread thread1 =new Thread(worker1);Thread thread2 =new Thread(worker2);Thread thread3 =new Thread(worker3);thread1.start();thread2.start();thread3.start();}}
public class Worker implements Runnable {private String name;private int time;private CyclicBarrier  barrier;public Worker(String name, int time,CyclicBarrier barrier) {super();this.name = name;this.time = time;this.barrier = barrier;}@Overridepublic void run() {try {Thread.sleep(time);System.out.println("task :"+name+":has already!");barrier.await();System.out.println("task :"+name+":run !");Thread.sleep(time);} catch (Exception e) {e.printStackTrace();}System.out.println("task :"+name+":complete!");}}
task :第二个任务:has already!
task :第一个任务:has already!
task :第三个任务:has already!
task :第二个任务:run !
task :第三个任务:run !
task :第一个任务:run !
task :第二个任务:complete!
task :第一个任务:complete!
task :第三个任务:complete!

5、区别:CyclicBarrier和CountDownLatch

Countdownlatch、CyclicBarrier、join区别相关推荐

  1. CountDownLatch和CyclicBarrier的区别

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

  2. 【Hive】left semi join(exists、in)和 left join 区别

    left semi join(exists.in)和 left join 区别 left semi join 基本认识 对比 执行计划 小结 left semi join 基本认识 LEFT SEMI ...

  3. ​left join 和 left semi join区别 ​

    左连接与+号, 就是左边的表数据都要. select * from a,b where a.id=b.id(+); (+)写在where后面,不能与or/in连用,ui select * from a ...

  4. join和countDownLatch原理及区别详解

    先上结论 原理 join 原理:在当前线程中调用另一个线程线程 thread 的 join() 方法时,会调用该 thread 的 wait() 方法,直到这个 thread 执行完毕(JVM在 ru ...

  5. 多线程之CountDownLatch和CyclicBarrier的区别和用法

    一.CountDownLatch的使用 CountDownLatch经常用于监听某些初始化操作,等初始化执行完毕后,再通知主线程继续工作. CountDownLatch定义: 一个同步辅助类,在完成一 ...

  6. 《Java 7 并发编程指南》学习概要 (3)Semaphore, CountDownLatch, CyclicBarrier , Phaser, Exchanger...

    1.Semaphore  信号量 Semaphore(信号量)是一个控制访问多个共享资源的计数器. 当一个线程想要访问某个共享资源,首先,它必须获得semaphore.如果semaphore的内部计数 ...

  7. LeetCode 1195. Fizz Buzz Multithreaded--并发系列题目--Java 解法--AtomicInteger/CountDownLatch/CyclicBarrier

    题目地址:Fizz Buzz Multithreaded - LeetCode Write a program that outputs the string representation of nu ...

  8. mysql sql left right inner join区别及效率比较

    一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona table1 ...

  9. mysql连接方式左联_数据库中的左连接(left join)和右连接(right join)区别 | 改变自己...

    Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...

最新文章

  1. ETSI GS MEC 015,MEP 带宽管理 API
  2. NB驱动层MQTT发布和订阅数据的代码实现
  3. 《大数据》2015年第2期“专题”——我国大数据交易的主要问题及建议
  4. 标准正态均一性检验 matlab程序,多种均一性检验方法比较研究
  5. 编码 GBK 的不可映射字符
  6. Java中四大代码块的执行顺序(附code)
  7. memcached全面剖析–memcached的删除机制和发展方向
  8. CTF 栅栏加密解密----python代码实现
  9. 服务器虚拟化的重要性,服务器虚拟化:虚拟机迁移的重要性
  10. 共识算法PBFT和Raft
  11. OMCS 语音视频框架
  12. 哪个邮箱登录安全?163 VIP邮箱登录入口是哪个?
  13. 浅析跨境电商行业为何发展如此迅猛?
  14. 用java代码取网名_【源码教程】iapp获取QQ昵称
  15. 欧姆龙SysmacStudio 关于模块化编程的使用技巧---全局变量和数据类型
  16. xp系统qq安装不上网络连接服务器,XP系统安装QQ提示无法访问Windows Installer的解决步骤...
  17. 【Linux伊甸园】JPEG病毒如何发作?都是内存地址惹的祸
  18. C++使用libCurl访问12306网站实现登录查询车次信息
  19. 关于人与人之间的那些烦恼
  20. [车联网安全自学篇] Car Hacking之车联网安全学习路线图

热门文章

  1. 前端,移动端开发框架
  2. 根据身份证号回填信息
  3. python导出_Python脚本导出为exe程序
  4. 如何写出高效率的sql语句
  5. linux proc/pid/stat解析
  6. 不知道玩什么游戏的你看过来
  7. 关于2048小游戏的开发感想
  8. 2019下半年第一个流行词---宏颜获水
  9. 七夕情人节表白网站代码 3D流星雨旋转相册 程序员专属情人节表白网站
  10. utools01-分享一个极简的多功能高效率工作神器