Countdownlatch、CyclicBarrier、join区别

本文深入探讨了Java中线程控制的四种主要方法:join、CountDownLatch、CyclicBarrier及分阶段控制策略。通过具体代码示例,对比分析了这些方法在控制线程同步与等待时的不同应用场景与特性。

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;
	}


	@Override
	public 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;
	}


	@Override
	public 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;
	}


	@Override
	public 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;
	}


	@Override
	public 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
在这里插入图片描述

内容概要:本文详细介绍了一个基于C++的养老院管理系统的设计与实现,旨在应对人口老龄化带来的管理挑战。系统通过整合住户档案、健康监测、护理计划、任务调度等核心功能,构建了从数据采集、清洗、AI风险预测到服务调度与可视化的完整技术架构。采用C++高性能服务端结合消息队列、规则引擎和机器学习模型,实现了健康状态实时监控、智能任务分配、异常告警推送等功能,并解决了多源数据整合、权限安全、老旧硬件兼容等实际问题。系统支持模块化扩展与流程自定义,提升了养老服务效率、医护协同水平和住户安全保障,同时为运营决策提供数据支持。文中还提供了关键模块的代码示例,如健康指数算法、任务调度器和日志记录组件。; 适合人群:具备C++编程基础,从事软件开发或系统设计工作1-3年的研发人员,尤其是关注智慧养老、医疗信息系统开发的技术人员。; 使用场景及目标:①学习如何在真实项目中应用C++构建高性能、可扩展的管理系统;②掌握多源数据整合、实时健康监控、任务调度与权限控制等复杂业务的技术实现方案;③了解AI模型在养老场景中的落地方式及系统架构设计思路。; 阅读建议:此资源不仅包含系统架构与模型描述,还附有核心代码片段,建议结合整体设计逻辑深入理解各模块之间的协同机制,并可通过重构或扩展代码来加深对系统工程实践的掌握。
内容概要:本文详细介绍了一个基于C++的城市交通流量数据可视化分析系统的设计与实现。系统涵盖数据采集与预处理、存储与管理、分析建模、可视化展示、系统集成扩展以及数据安全与隐私保护六大核心模块。通过多源异构数据融合、高效存储检索、实时处理分析、高交互性可视化界面及模块化架构设计,实现了对城市交通流量的实时监控、历史趋势分析与智能决策支持。文中还提供了关键模块的C++代码示例,如数据采集、清洗、CSV读写、流量统计、异常检测及基于SFML的柱状图绘制,增强了系统的可实现性与实用性。; 适合人群:具备C++编程基础,熟悉数据结构与算法,有一定项目开发经验的高校学生、研究人员及从事智能交通系统开发的工程师;适合对大数据处理、可视化技术和智慧城市应用感兴趣的技术人员。; 使用场景及目标:①应用于城市交通管理部门,实现交通流量实时监测与拥堵预警;②为市民出行提供路径优化建议;③支持交通政策制定与信号灯配时优化;④作为智慧城市建设中的智能交通子系统,实现与其他城市系统的数据协同。; 阅读建议:建议结合文中代码示例搭建开发环境进行实践,重点关注多线程数据采集、异常检测算法与可视化实现细节;可进一步扩展机器学习模型用于流量预测,并集成真实交通数据源进行系统验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值