个人学习笔记,如有错误欢迎指正。。
CyclicBarrier 类用于阻塞一个或多个线程,等待其它线程完成任务,直至所有线程都完成各自的任务,才会解除阻塞继续执行。
构建器 CyclicBarrier (int n,Runnable runnable)
await()方法被调用时,计数器N减一,如果计数据N>0,则当前线程阻塞,如果计数器N==0时,则 Runnable 被调用, Runnable 执行完成后,所有因为await()方法阻塞的线程被唤醒。N被再次重置为初始值,这个 CyclicBarrier 可以被再次复用。
示例:计算1+2+3+4+5+6+........+10000=??
由多个线程执行计算,每个线程计算的结果相加就等于最终结果,并每个线程打印出最终结果。
public class TestCyclicBarrier implements Runnable{ CyclicBarrier cyclicBarrier = null; int startValue; int endValue; AtomicLong total ; String threadName ; String resultReceiver; public TestCyclicBarrier(String threadName,CyclicBarrier cyclicBarrier,AtomicLong total,int startValue,int endValue,String resultReceiver){ this.cyclicBarrier = cyclicBarrier; this.startValue = startValue; this.endValue = endValue; this.total = total; this.threadName = threadName; this.resultReceiver = resultReceiver; } static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @Override public void run() { // TODO Auto-generated method stub try{ //开始计算 System.out.println(sdf.format(new Date())+": "+this.threadName+" count start:"+startValue+"+"+(startValue+1)+"+"+(startValue+2)+" .....+"+endValue+"=?"); Thread.sleep(2000); long total = 0; for(int i=startValue;i<endValue;i++){ total+= i; } System.out.println(sdf.format(new Date())+": "+this.threadName+" count finish:"+startValue+"+"+(startValue+1)+"+"+(startValue+2)+" .....+"+endValue+" = "+total); this.total.addAndGet(total);//计算完成 cyclicBarrier.await();//本线程计算工作完成,阻塞等待其它线程完成完成计算 System.out.println(sdf.format(new Date())+": "+this.threadName+" send result to "+resultReceiver+":"+this.total.get());//所有任务计算完成,将最终结果发送给某人 System.out.println(sdf.format(new Date())+": "+this.threadName+" to exit");//线程退出 }catch(Exception e){ e.printStackTrace(); } } public static void main(String args []){ final AtomicLong total = new AtomicLong(); int startValue = 1; int endValue = 10000; int difference = endValue-startValue; int threadNum = difference/1000;//每条线程计算1000个数据相加 if(difference%100>0){//计算需要线程数 threadNum++; } CyclicBarrier cyclicBarrier = new CyclicBarrier(threadNum,new Runnable(){ @Override public void run() {//把有线程任务执行完成时,调用。 // TODO Auto-generated method stub System.out.println(sdf.format(new Date())+": ALL thread count finish , count result ="+total.get()); } }); ExecutorService executorService=Executors.newCachedThreadPool(); for(int i=0;i<threadNum;i++){//任务拆分,发给多个线程执行 int startValueForThread = startValue+(i*1000); int endValueForThread = startValue+(i*1000)+1000; if(endValueForThread>endValue){ endValueForThread= endValue; } TestCyclicBarrier testCyclicBarrier = new TestCyclicBarrier("thread_"+i,cyclicBarrier,total,startValueForThread,endValueForThread,"person"+i); executorService.execute(testCyclicBarrier);//开始执行任务线程 } } }
程序输出:
2013-12-01 22:46:02: thread_1 count start:1001+1002+1003 .....+2001=?
2013-12-01 22:46:02: thread_0 count start:1+2+3 .....+1001=?
2013-12-01 22:46:02: thread_2 count start:2001+2002+2003 .....+3001=?
2013-12-01 22:46:02: thread_3 count start:3001+3002+3003 .....+4001=?
2013-12-01 22:46:02: thread_4 count start:4001+4002+4003 .....+5001=?
2013-12-01 22:46:02: thread_6 count start:6001+6002+6003 .....+7001=?
2013-12-01 22:46:02: thread_5 count start:5001+5002+5003 .....+6001=?
2013-12-01 22:46:02: thread_7 count start:7001+7002+7003 .....+8001=?
2013-12-01 22:46:02: thread_9 count start:9001+9002+9003 .....+10000=?
2013-12-01 22:46:02: thread_8 count start:8001+8002+8003 .....+9001=?
2013-12-01 22:46:04: thread_1 count finish:1001+1002+1003 .....+2001 = 1500500
2013-12-01 22:46:04: thread_0 count finish:1+2+3 .....+1001 = 500500
2013-12-01 22:46:04: thread_2 count finish:2001+2002+2003 .....+3001 = 2500500
2013-12-01 22:46:04: thread_3 count finish:3001+3002+3003 .....+4001 = 3500500
2013-12-01 22:46:04: thread_4 count finish:4001+4002+4003 .....+5001 = 4500500
2013-12-01 22:46:04: thread_5 count finish:5001+5002+5003 .....+6001 = 5500500
2013-12-01 22:46:04: thread_6 count finish:6001+6002+6003 .....+7001 = 6500500
2013-12-01 22:46:04: thread_7 count finish:7001+7002+7003 .....+8001 = 7500500
2013-12-01 22:46:04: thread_9 count finish:9001+9002+9003 .....+10000 = 9490500
2013-12-01 22:46:04: thread_8 count finish:8001+8002+8003 .....+9001 = 8500500
2013-12-01 22:46:04: ALL thread count finish , count result =49995000
2013-12-01 22:46:04: thread_8 send result to person8:49995000
2013-12-01 22:46:04: thread_8 to exit
2013-12-01 22:46:04: thread_1 send result to person1:49995000
2013-12-01 22:46:04: thread_1 to exit
2013-12-01 22:46:04: thread_2 send result to person2:49995000
2013-12-01 22:46:04: thread_2 to exit
2013-12-01 22:46:04: thread_3 send result to person3:49995000
2013-12-01 22:46:04: thread_3 to exit
2013-12-01 22:46:04: thread_5 send result to person5:49995000
2013-12-01 22:46:04: thread_6 send result to person6:49995000
2013-12-01 22:46:04: thread_9 send result to person9:49995000
2013-12-01 22:46:04: thread_0 send result to person0:49995000
2013-12-01 22:46:04: thread_9 to exit
2013-12-01 22:46:04: thread_6 to exit
2013-12-01 22:46:04: thread_5 to exit
2013-12-01 22:46:04: thread_4 send result to person4:49995000
2013-12-01 22:46:04: thread_4 to exit
2013-12-01 22:46:04: thread_7 send result to person7:49995000
2013-12-01 22:46:04: thread_0 to exit
2013-12-01 22:46:04: thread_7 to exit
相关推荐
6.4 深入理解 AQS 之 CyclicBarrie 详解副本.mp4
6.4 深入理解 AQS 之 CyclicBarrie 详解副本副本.mp4
AQS特性,以及源码流程,常见应用Semaphorer&CountDownLatch&CyclicBarrie
在Java并发编程中,CountDownLatch和CyclicBarrier是两种非常重要的同步工具类,它们用于协调多个线程间的协作。这两个工具都是在`java.util.concurrent`包下,是Java并发库的重要组成部分。 **CountDownLatch** ...
基于的手势识别系统可控制灯的亮_3
untitled2.zip
S7-1500和分布式外围系统ET200MP模块数据
anaconda配置pytorch环境
高校教室管理系统,主要的模块包括查看首页、个人中心、教师管理、学生管理、教室信息管理、教师申请管理、学生申请管理、课时表管理、教师取消预约管理、学生取消预约管理等功能。
半挂汽车列车横向稳定性控制研究:基于模糊PID与制动力矩分配的联合仿真分析在典型工况下的表现,半挂汽车列车在典型工况下的横向稳定性控制研究:基于模糊PID与制动力矩分配的联合仿真分析,半挂汽车列车4自由度6轴整车model,横向稳定性控制,在低附着系数路面,进行典型3个工况,角阶跃,双移线,方向盘转角。 采用算法:模糊PID,制动力矩分配,最优滑移率滑膜控制。 以上基于trucksim和simulink联合仿真,有对应 p-a-p-e-r参考 ,关键词: 1. 半挂汽车列车 2. 4自由度6轴整车model 3. 横向稳定性控制 4. 低附着系数路面 5. 典型工况(角阶跃、双移线、方向盘转角) 6. 模糊PID算法 7. 制动力矩分配 8. 最优滑移率滑膜控制 9. Trucksim和Simulink联合仿真 10. P-A-P-E-R参考; 用分号隔开上述关键词为:半挂汽车列车; 4自由度6轴整车model; 横向稳定性控制; 低附着系数路面; 典型工况; 模糊PID算法; 制动力矩分配; 最优滑移率滑膜控制; Trucksim和Simulink联合仿真; P-A-P-E-R参考
路径规划人工势场法及其改进算法Matlab代码实现,路径规划人工势场法及其改进算法Matlab代码实现,路径规划人工势场法以及改进人工势场法matlab代码,包含了 ,路径规划; 人工势场法; 改进人工势场法; MATLAB代码; 分隔词“;”。,基于Matlab的改进人工势场法路径规划算法研究
本文介绍了范德堡大学深脑刺激器(DBS)项目,该项目旨在开发和临床评估一个系统,以辅助从规划到编程的整个过程。DBS是一种高频刺激治疗,用于治疗运动障碍,如帕金森病。由于目标区域在现有成像技术中可见性差,因此DBS电极的植入和编程过程复杂且耗时。项目涉及使用计算机辅助手术技术,以及一个定制的微定位平台(StarFix),该平台允许在术前进行图像采集和目标规划,提高了手术的精确性和效率。此外,文章还讨论了系统架构和各个模块的功能,以及如何通过中央数据库和网络接口实现信息共享。
三菱FX3U步进电机FB块的应用:模块化程序实现电机换算,提高稳定性和移植性,三菱FX3U步进电机换算FB块:模块化编程实现电机控制的高效性与稳定性提升,三菱FX3U 步进电机算FB块 FB块的使用可以使程序模块化简单化,进而提高了程序的稳定性和可移植性。 此例中使用FB块,可以实现步进电机的算,已知距离求得脉冲数,已知速度可以求得频率。 程序中包含有FB和ST内容;移植方便,在其他程序中可以直接添加已写好的FB块。 ,三菱FX3U;步进电机换算;FB块;程序模块化;稳定性;可移植性;距离与脉冲数换算;速度与频率换算;FB和ST内容;移植方便。,三菱FX3U步进电机换算FB块:程序模块化与高稳定性实现
光伏逆变器TMS320F28335设计方案:Boost升压与单相全桥逆变,PWM与SPWM控制,MPPT恒压跟踪法实现,基于TMS320F28335DSP的光伏逆变器设计方案:Boost升压与单相全桥逆变电路实现及MPPT技术解析,光伏逆变器设计方案TMS320F28335-176资料 PCB 原理图 源代码 1. 本设计DC-DC采用Boost升压,DCAC采用单相全桥逆变电路结构。 2. 以TI公司的浮点数字信号控制器TMS320F28335DSP为控制电路核心,采用规则采样法和DSP片内ePWM模块功能实现PWM和SPWM波。 3. PV最大功率点跟踪(MPPT)采用了恒压跟踪法(CVT法)来实现,并用软件锁相环进行系统的同频、同相控制,控制灵活简单。 4.资料包含: 原理图,PCB(Protel或者AD打开),源程序代码(CCS打开),BOM清单,参考资料 ,核心关键词:TMS320F28335-176; 光伏逆变器; 升压; 逆变电路; 数字信号控制器; 规则采样法; ePWM模块; PWM; SPWM波; MPPT; 恒压跟踪法; 原理图; PCB; 源程序代码; BOM
centos9内核安装包
昆仑通态触摸屏与两台台达VFD-M变频器通讯实现:频率设定、启停控制与状态指示功能接线及设置说明,昆仑通态TPC7062KD触摸屏与两台台达VFD-M变频器通讯程序:实现频率设定、启停控制与状态指示,昆仑通态MCGS与2台台达VFD-M变频器通讯程序实现昆仑通态触摸屏与2台台达VFD-M变频器通讯,程序稳定可靠 器件:昆仑通态TPC7062KD触摸屏,2台台达VFD-M变频器,附送接线说明和设置说明 功能:实现频率设定,启停控制,实际频率读取等,状态指示 ,昆仑通态MCGS; 台达VFD-M变频器; 通讯程序; 稳定可靠; 频率设定; 启停控制; 实际频率读取; 状态指示; 接线说明; 设置说明,昆仑通态MCGS与台达VFD-M变频器通讯程序:稳定可靠,双机控制全实现
研控步进电机驱动器方案验证通过,核心技术成熟可生产,咨询优惠价格!硬件原理图与PCB源代码全包括。,研控步进电机驱动器方案验证通过,核心技术掌握,生产准备,咨询实际价格,包含硬件原理图及PCB源代码。,研控步进电机驱动器方案 验证可用,可以生产,欢迎咨询实际价格,快速掌握核心技术。 包括硬件原理图 PCB源代码 ,研控步进电机驱动器方案; 验证可用; 可生产; 核心技术; 硬件原理图; PCB源代码,研控步进电机驱动器方案验证通过,现可生产供应,快速掌握核心技术,附硬件原理图及PCB源代码。
高质量的OPCClient_UA源码分享:基于C#的OPC客户端开发源码集(测试稳定、多行业应用实例、VS编辑器支持),高质量OPC客户端源码解析:OPCClient_UA C#开发,适用于VS2019及多行业现场应用源码分享,OPCClient_UA源码OPC客户端源码(c#开发) 另外有opcserver,opcclient的da,ua版本的见其他链接。 本项目为VS2019开发,可用VS其他版本的编辑器打开项目。 已应用到多个行业的几百个应用现场,长时间运行稳定,可靠。 本项目中提供测试OPCClient的软件开发源码,有详细的注释,二次开发清晰明了。 ,OPCClient_UA; OPC客户端源码; C#开发; VS2019项目; 稳定可靠; 详细注释; 二次开发,OPC客户端源码:稳定可靠的C#开发实现,含详细注释支持二次开发
毕业设计