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

本文通过模拟田径比赛场景,详细介绍了Java中的CountDownLatch和CyclicBarrier等线程同步工具的使用方法,展示了如何在多线程环境下实现有序执行和等待任务完成的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

案例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();

	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值