My study on CyclicBarrier

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值