0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12723
1 重点关注
1.1 本节内容
CountDownLatch代码应用,详细参见3.1
1.2 关键代码
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)); // 使用线程池,3个线程池,和调度站对应 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; }
@Override public void run() { try { check(); ok = true; } catch (Exception e) { e.printStackTrace(); ok = false; } finally { if (countDown != null) { countDown.countDown(); } } }
2 课程内容
3 Coding
3.1 CountDownLatch代码应用
- 线程调度类
package com.imooc.countdown; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; 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); } }
- 主类
package com.imooc.countdown; import java.util.concurrent.CountDownLatch; /** * 抽象类,用于演示 危险品化工车监控中心 统一检查 */ 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; } }
- 子类1
package com.imooc.countdown; import java.util.concurrent.CountDownLatch; public class StationBeijingIMooc extends DangerCenter { public StationBeijingIMooc(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() + "] 完毕,可以发车~"); } }
- 子类2
package com.imooc.countdown; import java.util.concurrent.CountDownLatch; 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() + "] 完毕,可以发车~"); } }
- 子类3
package com.imooc.countdown; import java.util.concurrent.CountDownLatch; 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() + "] 完毕,可以发车~"); } }
- 打印日志
正在检查 [北京慕课调度站]... 正在检查 [山东长川调度站]... 正在检查 [江苏三林调度站]... 检查 [山东长川调度站] 完毕,可以发车~ 检查 [江苏三林调度站] 完毕,可以发车~ 检查 [北京慕课调度站] 完毕,可以发车~ 监控中心针对所有危化品调度站点的检查结果为:true
- 验证部分
通过debug方式验证:
以上图片代码部分执行3次,才开始执行await方法之后的代码
验证成功
诸葛