jdk1.5——CountdownLatch(计数器线程工具同步类)

案例1: 100米田径中,裁判员1鸣枪后,运动员跑,后所有运动员到达终点,裁判员2统计结果:

package thread;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 执行结果:
 * 倒计时3秒
倒计时2秒
倒计时1秒
裁判员1鸣枪
运动员pool-1-thread-2 begin run
运动员pool-1-thread-4 begin run
运动员pool-1-thread-5 begin run
运动员pool-1-thread-3 begin run
运动员pool-1-thread-2 end run
运动员pool-1-thread-4 end run
运动员pool-1-thread-5 end run
运动员pool-1-thread-3 end run
裁判员2pool-1-thread-6统计完结果
 * @author zm
 *
 */
public class CountDownLatchTest2 {

	/**
	 * 裁判员1发令结束
	 * 运动员1,2,3,4开始跑
	 * 裁判员2在运动员都跑到终点后,统计名词
	 * ------> 执行阶段性任务
	 */
	public static void main(String[] args) {

		// 因为有三个阶段任务,因此需要两个阶段任务间隔计数器
		final CountDownLatch countDownLatch1 = new CountDownLatch(1);
		final CountDownLatch countDownLatch2 = new CountDownLatch(4);
		
		ExecutorService service = Executors.newCachedThreadPool();  
		
		//0 裁判员线程
		service.execute(new Runnable() {
			@Override
			public void run() {
				for(int i=3; i>=1; i--){
					try {
						TimeUnit.SECONDS.sleep(1);
						System.out.println("倒计时" + i + "秒");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("裁判员1鸣枪");
				countDownLatch1.countDown();
			}
		});
		
	
		// 1 4个运动员线程
		for(int i=0; i<4; i++){
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					try {
						countDownLatch1.await();
						System.out.println("运动员" + Thread.currentThread().getName() + " begin run");
						TimeUnit.SECONDS.sleep(new Random().nextInt(3));
						System.out.println("运动员" + Thread.currentThread().getName() + " end run");
						countDownLatch2.countDown();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			};
			service.execute(runnable);
		}
		
		// 2 裁判员统计结果
		service.execute(new Runnable() {
			@Override
			public void run() {
				try {
					countDownLatch2.await();
					System.out.println("裁判员2" + Thread.currentThread().getName() + "统计完结果");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		});
	 	
	}

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

/**
 * 计数器线程工具同步类
 * @author zm
 * 
 * 线程pool-1-thread-1正准备接受命令
线程pool-1-thread-2正准备接受命令
线程pool-1-thread-3正准备接受命令
线程main即将发布命令
线程main已发送命令,正在等待结果
线程pool-1-thread-1已接受命令
线程pool-1-thread-2已接受命令
线程pool-1-thread-3已接受命令
 *
 */
public class CountdownLatchTest {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		final CountDownLatch cdOrder = new CountDownLatch(1);
		final CountDownLatch cdAnswer = new CountDownLatch(3);		
		for(int i=0;i<3;i++){
			Runnable runnable = new Runnable(){
					public void run(){
					try {
						System.out.println("线程" + Thread.currentThread().getName() + 
								"正准备接受命令");						
						cdOrder.await();
						System.out.println("线程" + Thread.currentThread().getName() + 
						"已接受命令");								
						Thread.sleep((long)(Math.random()*10000));	
						System.out.println("线程" + Thread.currentThread().getName() + 
								"回应命令处理结果");						
						cdAnswer.countDown();						
					} catch (Exception e) {
						e.printStackTrace();
					}				
				}
			};
			service.execute(runnable);
		}		
		try {
			Thread.sleep((long)(Math.random()*10000));
		
			System.out.println("线程" + Thread.currentThread().getName() + 
					"即将发布命令");						
			cdOrder.countDown();
			System.out.println("线程" + Thread.currentThread().getName() + 
			"已发送命令,正在等待结果");	
			cdAnswer.await();
			System.out.println("线程" + Thread.currentThread().getName() + 
			"已收到所有响应结果");	
		} catch (Exception e) {
			e.printStackTrace();
		}				
		service.shutdown();

	}
}

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2145546