CountDownLatch使用

在这里插入图片描述
在这里插入图片描述

定义一个具有初始值CountDownLatch,各个子线程进行累减,子线程中的CountDownLatch到0,主线程的await才能会被唤醒继续进行

/**
 * 抽象类,用于演示 危险品化工车监控中心 统一检查
 */
public abstract class DangerCenter implements Runnable {

	private CountDownLatch countDown;		// 计数器
	private String station;					// 调度站
	private boolean ok;						// 调度站针对当前自己的站点进行检查,是否检查ok的标志
	
	public DangerCenter(CountDownLatch countDown, String station) {
		this.countDown = countDown;
		this.station = station;
		this.ok = false;
	}

	@Override
	public void run() {
		try {
			check();
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
			ok = false;
		} finally {
			if (countDown != null) {
				countDown.countDown();
			}
		}
	}

	/**
	 * 检查危化品车
	 * 蒸罐
	 * 汽油
	 * 轮胎
	 * gps
	 * ...
	 */
	public abstract void check();

	public CountDownLatch getCountDown() {
		return countDown;
	}
	public void setCountDown(CountDownLatch countDown) {
		this.countDown = countDown;
	}
	public String getStation() {
		return station;
	}
	public void setStation(String station) {
		this.station = station;
	}
	public boolean isOk() {
		return ok;
	}
	public void setOk(boolean ok) {
		this.ok = ok;
	}
	
}

public class StationBeijing extends DangerCenter {

	public StationBeijing(CountDownLatch countDown) {
		super(countDown, "北京调度站");
	}

	@Override
	public void check() {
		System.out.println("正在检查 [" + this.getStation() + "]...");
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("检查 [" + this.getStation() + "] 完毕,可以发车~");
	}

}


public class StationJiangsuSanling extends DangerCenter {

	public StationJiangsuSanling(CountDownLatch countDown) {
		super(countDown, "江苏三林调度站");
	}

	@Override
	public void check() {
		System.out.println("正在检查 [" + this.getStation() + "]...");
		
		try {
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("检查 [" + this.getStation() + "] 完毕,可以发车~");
	}

}

public class StationShandongChangchuan extends DangerCenter {

		public StationShandongChangchuan(CountDownLatch countDown) {
			super(countDown, "山东长川调度站");
		}

		@Override
		public void check() {
			System.out.println("正在检查 [" + this.getStation() + "]...");

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			System.out.println("检查 [" + this.getStation() + "] 完毕,可以发车~");
		}

}

public class CheckStartUp {

	private static List<DangerCenter> stationList;
	private static CountDownLatch countDown;
	
	public CheckStartUp() {
	}
	
	public static boolean checkAllStations() throws Exception {

		// 初始化3个调度站
		countDown = new CountDownLatch(3);
		
		// 把所有站点添加进list
		stationList = new ArrayList<>();
		stationList.add(new StationBeijingIMooc(countDown));
		stationList.add(new StationJiangsuSanling(countDown));
		stationList.add(new StationShandongChangchuan(countDown));
		
		// 使用线程池
		Executor executor = Executors.newFixedThreadPool(stationList.size());
		
		for (DangerCenter center : stationList) {
			executor.execute(center);
		}
		
		// 等待线程执行完毕
		countDown.await();
		
		for (DangerCenter center : stationList) {
			if (!center.isOk()) {
				return false;
			}
		}
		
		return true;
	}
	
	public static void main(String[] args) throws Exception {
		boolean result = CheckStartUp.checkAllStations();
		System.out.println("监控中心针对所有危化品调度站点的检查结果为:" + result);
	}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值