`
jay_kid
  • 浏览: 63885 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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?

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics