线程并发工具--CyclicBarrier
来源:互联网 发布:淘宝泄露买家信息 编辑:程序博客网 时间:2024/05/18 03:15
CyclicBarrier能实现的效果是多个线程同时执行,这些线程执行的时间不一。但是要求在某一个点上,需要这些线程都执行完毕了之后,全部线程才能往下执行。
下面是示例程序:
ExecutorService service = Executors.newCachedThreadPool();final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);for(int i = 0;i<3;i++){Runnable runnable = new Runnable(){Random random = new Random();@Overridepublic void run() {try {Thread.sleep(random.nextInt(5000));System.out.println("point 1:"+Thread.currentThread().getName()+" execute complate.");cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}try {Thread.sleep(random.nextInt(5000));System.out.println("point 2:"+Thread.currentThread().getName()+" execute complate.");cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}try {Thread.sleep(random.nextInt(5000));System.out.println("point 3:"+Thread.currentThread().getName()+" execute complate.");cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}try {Thread.sleep(random.nextInt(5000));System.out.println("point 4:"+Thread.currentThread().getName()+" execute complate.");cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}}};service.execute(runnable);//service.shutdown();}执行结果:
point 1:pool-1-thread-3 execute complate.point 1:pool-1-thread-2 execute complate.point 1:pool-1-thread-1 execute complate.point 2:pool-1-thread-2 execute complate.point 2:pool-1-thread-1 execute complate.point 2:pool-1-thread-3 execute complate.point 3:pool-1-thread-3 execute complate.point 3:pool-1-thread-1 execute complate.point 3:pool-1-thread-2 execute complate.point 4:pool-1-thread-1 execute complate.point 4:pool-1-thread-2 execute complate.point 4:pool-1-thread-3 execute complate.可以看到,到达么一个点的顺序可能不固定,但是没有说一个线程才到达point1的时候,另外一个线程已经达到point2了,即大家共同达到一个点之后才会继续往下执行。
0 0
- 线程并发工具--CyclicBarrier
- 【java并发】线程同步工具CyclicBarrier的使用
- Java线程(CountDownLatch、CyclicBarrier、Semaphore)并发控制工具类
- CyclicBarrier线程同步工具
- 线程同步工具-CyclicBarrier
- Java并发工具类CyclicBarrier
- java 并发工具类-CyclicBarrier
- Java并发学习(十六)-并发工具CyclicBarrier
- 线程同步工具类--CyclicBarrier
- 线程同步工具之CyclicBarrier
- Java线程同步工具-CyclicBarrier
- java 线程 同步工具 CyclicBarrier
- Java并发学习之十八——线程同步工具之CyclicBarrier
- Java_并发线程_Semaphore、CountDownLatch、CyclicBarrier、Exchanger
- 用CountDownLatch和CyclicBarrier处理并发线程
- java 并发工具类CyclicBarrier介绍
- Java并发工具类CountDownLatch和CyclicBarrier
- Java并发之工具类CyclicBarrier
- 网站搭建从零开始(四) 服务器的配置
- hdu 5046 重复覆盖
- (第四章)Unity3D-地球旋转Dome
- 一个简单web服务器的java实现
- ios开发中打印日志消息控制
- 线程并发工具--CyclicBarrier
- 06-Eclipse MyEclipse 复制项目 复制现有项目 复制功能相似项目
- Minimum Depth of Binary Tree【leetcode】
- PHP在linux上执行外部命令(整理)
- poj-3253
- vim集成ctags和taglist
- 目前博客已转移到cnblog,欢迎围观~~~
- Hdu 2167 Pebbles 状态压缩
- ACM Radar Installation(挑战程序设计竞赛)