package com.jaykid.test.java.thread;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class MyCyclicBarrierTest {
private static CyclicBarrier barrier;
public static void main(String[] args) {
barrier = new CyclicBarrier(4, new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getId() + ": I'm the latest! Wooow~~~");
}
});
new TravelingGroup("Walk", 5000L).start();
new TravelingGroup("Bike", 3000L).start();
new TravelingGroup("Taxi", 1000L).start();
new TravelingGroup("Plain", 500L).start();
}
/**
* Inner class to simulate a traveling group
*
*/
private static class TravelingGroup extends Thread {
private final long timeOnTheWay;
private final String groupName;
public TravelingGroup(String groupName, long timeOnTheWay) {
this.groupName = groupName;
this.timeOnTheWay = timeOnTheWay;
}
@Override
public void run() {
try {
movingVeryHard();
trace("Reach Guangzhou! So many delicious food to eat!");
barrier.await();
movingVeryHard();
trace("Reach ShenZhen! Where is Hawking?");
barrier.await();
movingVeryHard();
trace("Reach ShangHai! The traffic is crazy!");
barrier.await();
movingVeryHard();
trace("Reach BeiJing! Snowing... very cold !!Bye Bye!");
} catch (InterruptedException e) {
trace("catch InterruptedException");
} catch (BrokenBarrierException e) {
trace("catch BrokenBarrierException");
}
}
public void trace(String trace) {
System.out.println(groupName + "[" + Thread.currentThread().getId() + "]" + " : " + trace);
}
private void movingVeryHard() throws InterruptedException {
Thread.sleep(timeOnTheWay);
}
}
}
1. What is CyclicBarrier
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.
2. Code example
Three Group of people are travlling, they start from HK, then pass GuangZhou, ShenZhen, ShangHai, finally reach Beijing. they set a rule, that they will set out to next destination only when all the member reach current location. Let's use Java to describe this exciting event.
3. Can I execute some logic when the barrier point reach, which thread will execute that ?
A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. The last thread which call await() will execute that runnable.
4. What kind of Exception will it throw
The CyclicBarrier uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).
5. How the CyclicBarrier was implemented?
* How the thrad keep blck?
* How the last thread invoke all the waiting thread?
* How it execute the runnable?
分享到:
相关推荐
CyclicBarrier的使用以及注意事项
《java并发编程》中CountDownLatch和CyclicBarrier用法实例大全,几乎包含了所有重要的用法
CyclicBarrier是Java中的一个同步工具类,用于协调多个线程之间的同步。它的作用类似于CountDownLatch,可以让一个或多个线程等待其他线程执行完毕后再继续执行。 CyclicBarrier可以被看作是一个屏障,当所有线程都...
NULL 博文链接:https://hubowei1.iteye.com/blog/2312471
用CyclicBarrier,reentrantlock,condition来完成同时购买,同步购买的功能 JUC系列之模拟抢票(N人同时抢票,票不足系统补仓,N-M人继续抢票) http://blog.csdn.net/crazyzxljing0621/article/details/77891620
主要为大家详细分析了Java并发系列之CyclicBarrier源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
1.2 CyclicBarrier概述 CyclicBarrier的基本用法 2.1 创建CyclicBarrier对象 2.2 await()方法 控制线程协作数量 3.1 场景介绍 3.2 使用CyclicBarrier控制线程协作 循环使用CyclicBarrier 4.1 场景介绍 4.2 使用...
在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时CyclicBarrier很有用。因为该barrier在释放等待线程后可以重用,所以称它为 循环 的barrier。 CyclicBarrier 支持一个可选的 Runnable命令,...
主要介绍了Java并发实例之CyclicBarrier的使用,涉及CyclicBarrier的介绍,以及相关的并发实例,具有一定参考价值,需要的朋友可以了解下。
CyclicBarrier和CountDownLatch一样,都是关于线程的计数器。用法略有不同,测试代码如下:
CyclicBarrier类似于CountDownLatch也是个计数器,不同的是CyclicBarrier数的是调用了CyclicBarrier.await()进入等待的线程数,当线程数达到了CyclicBarrier初始时规定的数目时,所有进入等待状态的线程被唤醒并继续...
java并发编程中CountDownLatch和CyclicBarrier的使用.pdf
java并发编程中CountDownLatch和CyclicBarrier的使用借鉴.pdf
主要介绍了java多线程之CyclicBarrier的使用方法的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下
主要介绍了Java并发编程(CyclicBarrier)实例详解的相关资料,JAVA编写并发程序的时候,我们需要仔细去思考一下并发流程的控制,如何让各个线程之间协作完成某项工作。
CountDownLatch 和 CyclicBarrier 为线程同步的辅助工具,通过它可以做到使一条线程一直阻塞等待,直到其他线程完成其所处理的任务。
CyclicBarrier实现多个线程相互等待的案例(实现累加操作)