定义一个具有初始值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);
}
}