cyclicbarrier如何让一段程序并发的执行并最终汇总结果

cyclicbarrier  时间:2021-07-16  阅读:()

分库的数据用java怎么并行查询

如果使用java多线程查询,必须使用CyclicBarrier,等待所有结果都查询完成合并结果集,或者使用dblink,然后HINT方式使用并行Parallell,利用数据的并行技术进行查询

求大神,java中同步器的使用方法、

同步器Semaphore(一) 同步器CyclicBarrier(二) 同步器CountDownLatch(三) 同步器Exchanger(四) 想了解详细:/category/149927

java中怎样实现多线程执行的结果相加

import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** 单独的计算线程,比如计算{1...10}的相加 @author zhaohb */ public class CounterThread extends Thread{ private int start; private int end; private CyclicBarrier barrier ; public CounterThread(int id,int start, int end,CyclicBarrier barrier) { this.start = start; this.end = end; this.barrier = barrier; setName("Thread-"+id+" "); } @Override public void run() { int count = 0; for(int i=start;i<end+1;i++){ count += i; } Counter.totalCount(count); try { barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** 测试类入口 @author zhaohb */ public class CounterTest { public static void main(String[] args) { //CyclicBarrier指定了当10个线程运行结束时候,可以进行最后结果展示了 CyclicBarrier barrier = new CyclicBarrier(10,new TotalTask(new Counter())); ExecutorService executorService = Executors.newCachedThreadPool(); for(int i=0;i<10;i++){ int start = i*10+1; int end = start + 9; CounterThread counterThread = new CounterThread(i,start, end,barrier); executorService.execute(counterThread); } executorService.shutdown(); } } /** 线程结果计算:将单独的线程的计算的结果相加,汇总的到总的结果 @author zhaohb * */ class Counter { private static int count =0; public synchronized static int totalCount(int perCount){ count += perCount; return count; } public int totalResult(){ return count; } } /** 最后结算展示线程 @author zhb * */ class TotalTask implements Runnable{ private Counter counter ; public TotalTask(Counter counter){ this.counter = counter; } @Override public void run() { System.out.println("所有线程运行完毕,总结果为:"); int total = counter.totalResult(); System.out.println(total); } }

CountDownLatch和CyclicBarrier的区别

这两天写多线程时,用到了CyclicBarrier,下意识的认为CyclicBarrier和CountDownLatch作用很像,就翻阅资料查了一下,说一下他们的区别吧 CyclicBarrier和CountDownLatch 都位于java.util.concurrent 这个包下 CountDownLatch CyclicBarrier 减计数方式 加计数方式 计算为0时释放所有等待的线程 计数达到指定值时释放所有等待线程 计数为0时,无法重置 计数达到指定值时,计数置为0重新开始 调用countDown()方法计数减一 调用await()方法只进行阻塞 对计数没任何影响 调用await()方法计数加1 若加1后的值不等于构造方法的值 则线程阻塞 不可重复利用 可重复利用

如何让一段程序并发的执行并最终汇总结果

使用CyclicBarrier 和CountDownLatch都可以,使用CyclicBarrier 在多个关口处将多个线程执行结果汇总,CountDownLatch 在各线程执行完毕后向总线程汇报结果。

TTcloud:日本独立服务器促销活动,价格$70/月起,季付送10Mbps带宽

ttcloud怎么样?ttcloud是一家海外服务器厂商,运营服务器已经有10年时间,公司注册地址在香港地区,业务范围包括服务器托管,机柜托管,独立服务器等在内的多种服务。我们后台工单支持英文和中文服务。TTcloud最近推出了新上架的日本独立服务器促销活动,价格 $70/月起,季付送10Mbps带宽。也可以跟进客户的需求进行各种DIY定制。点击进入:ttcloud官方网站地址TTcloud拥有自...

VoLLcloud:超便宜香港CMI大带宽vps-三网CMI直连-年付四免服务-低至4刀/月-奈飞

vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。致力于全球服务器租用、托管及云计算、DDOS安 全防护、数据实时存储、 高防服务器加速、域名、智能高防服务器、网络安全服务解决方案等领域的智 能化、规范化的体验服务。所有购买年付产品免费更换香港原生IP(支持解锁奈飞),商家承诺,支持3天内无条件退款(原路退回)!点击进入:vollclo...

Hostodo:4款便宜美国vps七折优惠低至$13/年;NVMe阵列1Gbps带宽,免费DirectAdmin授权

hostodo怎么样?快到了7月4日美国独立日,hostodo现在推出了VPS大促销活动,提供4款Hostodo美国独立日活动便宜VPS,相当于7折,低至$13/年,续费同价。Hostodo美国独立日活动结束时间不定,活动机售完即止。Hostodo商家支持加密数字货币、信用卡、PayPal、支付宝、银联等付款。Hostodo美国独立日活动VPS基于KVM虚拟,NVMe阵列,1Gbps带宽,自带一个...

cyclicbarrier为你推荐
ipv6无网络访问权限电信本地连接IPv6无网络访问权限是什么意思firstname中国人填资料时,first name是名还是姓?微信收款语音播报怎么设置怎么设置农商银行的收钱语音播报?showwindowvb ShowWindow 的使用方法 隐藏/显示 指定窗口 (完整代码)谢谢李昊天铠甲勇士2刑天的李昊天秘密是什么欢迎页面怎样取消“欢迎页面”?pci数据捕获和信号处理控制器三星450r5v x05 设备管理器出现pci数据捕获和信号处理控制器有个黄色的感叹号怎么办什么是SOA什么是中子刀?您了解吗?新手怎么制作表格怎么制作表格?easeljs如何使用createjs来编写html5游戏
域名备案流程 ftp空间 日本软银 technetcal yardvps jsp主机 流媒体服务器 512av realvnc css样式大全 地址大全 租空间 元旦促销 免费申请个人网站 台湾谷歌 如何注册阿里云邮箱 电信托管 空间登录首页 台湾google 备案空间 更多