jdk1.5——线程同步类CyclicBarrier

 

 

 

案例1:

package cn.crxy.synchronizer;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
// 和CountDownLatch类似 但是使用场景不同, CountDownLatch用于 组和组线程之间等待   CyclicBarrierDemo用于同组内线程相互等待 
public class CyclicBarrierDemo {

	public static void main(String[] args) {
		final CyclicBarrier cb = new CyclicBarrier(4);
		
		for (int i = 0; i < 4; i++) {
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					
					try {
						System.out.println(Thread.currentThread().getName()+" :爬山。。。");
						TimeUnit.SECONDS.sleep(new Random().nextInt(5)+1);
						System.out.println(Thread.currentThread().getName()+" :到达山顶了。。。");
						cb.await();//等待,直到计数器变为0为止
						
						System.out.println(Thread.currentThread().getName()+" :吃饭。。。");
						TimeUnit.SECONDS.sleep(new Random().nextInt(5)+1);
						System.out.println(Thread.currentThread().getName()+" :吃完饭了。。。");
						cb.await();//等待,直到计数器变为0为止
						
						System.out.println(Thread.currentThread().getName()+" :KTV。。。");
						TimeUnit.SECONDS.sleep(new Random().nextInt(5)+1);
						System.out.println(Thread.currentThread().getName()+" :唱完歌了。。。");
						cb.await();//等待,直到计数器变为0为止
						
						System.out.println(Thread.currentThread().getName()+":回家");
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					
				}
			}).start();
		}
		
	}
}

 

 

 

案例2:

 

 

package cn.itcast.heima2;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 场景: 各自家出发到公司,会和后各自去公园,都达到公园会和后,在去A饭店吃饭
 * 
 * 
 * @author zm
 *
 */
public class CyclicBarrierTest {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		final  CyclicBarrier cb = new CyclicBarrier(3);
		for(int i=0;i<3;i++){
			Runnable runnable = new Runnable(){
					public void run(){
					try {
						Thread.sleep((long)(Math.random()*1000));	
						System.out.println("线程" + Thread.currentThread().getName() + 
								"即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));						
						cb.await();
						
						Thread.sleep((long)(Math.random()*1000));	
						System.out.println("线程" + Thread.currentThread().getName() + 
								"即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
						cb.await();	
						Thread.sleep((long)(Math.random()*1000));	
						System.out.println("线程" + Thread.currentThread().getName() + 
								"即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));						
						cb.await();						
					} catch (Exception e) {
						e.printStackTrace();
					}				
				}
			};
			service.execute(runnable);
		}
		service.shutdown();
	}
}

 

你可能感兴趣的:(Cyclicbarrier)