个人学习笔记,如有错误欢迎指正。。
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用法实例大全,几乎包含了所有重要的用法
基于matlab实现的空间调制通信过程,包含信号调制、天线选择等发送过程,以及采用最大似然估计的检测过程。.rar
本文档是课题研究的研究报告内含调研以及源码设计以及结果分析
网络作为特征提取器-python源码.zip
JavaScript-javaweb项目
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
触屏版自适应手机wap软件网站模板 触屏版自适应手机wap软件网站模板
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
IEC 60695-11-3:2012.pdf
上市公司和讯网社会责任评级CSR-股东责任员工责任客户消费者环境社会责任分项评级 得分(2010-2021年) "中国上市公司-和讯网社会责任数据"是 一份来自和讯网的数据集,它同步并收集了中国上市公司关于社会责任的相关信息。包括了 公司在股东责任、员工责任、供应商客户消费者权益责任、环境责任、社会责任中的表现和 成绩,以反映公司承担社会责任的程度。可以帮助大家了解公司在承担社会责任方面的具体 表现。这对于研究公司社会责任与公司业绩、公司声誉、公司风险等方面的关系具有参考意 义。 一、数据介绍 数据名称:上市公司和讯网社会责任评级CSR-股东责任员工责任 客户消费者环境社会责任分项评级得分 数据年份:2010-2021年 样本数量:每 年含2300-4600左右上市公司数据,总数据量40058条(注:因披露口径原因 ,2021年仅有430+上市公司数据) 数据格式:Excel面板数据 二、指标说 明 共计11个指标:股票名称、股票代码、年份、总得分、等级、股东责任、员工责任、 供应商客户和消费者权益责任、环境责任、社会责任、统计日期 三、部分excel数据 展示
使用opencv进行人脸识别和对比-python源码.zip
EmotionVGGnet情绪识别-python源码.zip
Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
MediaPipe人体姿势估计-python源码.zip
构造并使用决策树进行分类-python源码.zip
<2024年5月软考高项极限冲刺>《1 考试简介》
【特效超多】仿德国开元旅游触屏版html5手机wap旅游网站模板下载.zip