CountDownLatch在java.util.concurrent.CountDownLatch包里面,他通常用在多線程中,假如說我們現(xiàn)在有一件事情開了多線程去完成,但是我們需要保證所有的線程都成功運(yùn)行結(jié)束,再去執(zhí)行后面的代碼,這個(gè)時(shí)候CountDownLatch這個(gè)類就派上用場了。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、嘉魚網(wǎng)絡(luò)推廣、微信小程序開發(fā)、嘉魚網(wǎng)絡(luò)營銷、嘉魚企業(yè)策劃、嘉魚品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供嘉魚建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
我們可以把CountDownLatch傳入到每個(gè)線程里面去,在線程執(zhí)行成功時(shí)調(diào)用countDownLatch.countDown();方法,然后在主線程里面執(zhí)行完多線程初始化和分發(fā)執(zhí)行之后執(zhí)行countDownLatch.await();這樣,只有在每個(gè)線程執(zhí)行成功以后,也就是CountDownLatch的計(jì)數(shù)器減到0以后,才會執(zhí)行countDownLatch.await()后面的代碼。
簡單的應(yīng)用場景是這樣的,但是具體的怎么設(shè)計(jì)還是需要仔細(xì)斟酌,否則有可能會出現(xiàn)countDownLatch.countDown();永遠(yuǎn)無法減為0的情況。
下面一個(gè)簡單的例子來實(shí)現(xiàn)這一應(yīng)用場景:
package org.hzg.countDownLatch; import java.util.concurrent.CountDownLatch; /** * Created by hzgal on 2019-3-5. */ public abstract class DangerCenter implements Runnable { private CountDownLatch countDownLatch; //計(jì)數(shù)器 private String station; //危險(xiǎn)品調(diào)度站 private boolean isOkToSentCar; //檢查結(jié)果,是否滿足發(fā)車條件 public DangerCenter(CountDownLatch countDownLatch,String stationName) { this.countDownLatch = countDownLatch; this.station = stationName; this.isOkToSentCar = false; } @Override public void run() { try { check(); isOkToSentCar = true; }catch (Exception e) { e.printStackTrace(); isOkToSentCar = false; }finally { if(countDownLatch != null) { countDownLatch.countDown(); } } } /** * 定義抽象的check方法,用于實(shí)現(xiàn)了DangerCenter的各級調(diào)度中心來重寫check方法,因?yàn)槊總€(gè)調(diào)度中心自檢的方法可能不一樣 */ public abstract void check(); public CountDownLatch getCountDownLatch() { return countDownLatch; } public void setCountDownLatch(CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch; } public String getStation() { return station; } public void setStation(String station) { this.station = station; } public boolean isOkToSentCar() { return isOkToSentCar; } public void setOkToSentCar(boolean okToSentCar) { isOkToSentCar = okToSentCar; } } |
package org.hzg.countDownLatch; import java.util.concurrent.CountDownLatch; /** * Created by hzgal on 2019-3-5. */ public class BeijingStation extends DangerCenter { private static String stationName = "北京危險(xiǎn)品調(diào)度站"; public BeijingStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("北京調(diào)度站正在進(jìn)行車輛自檢.........."); try { Thread.sleep(2000); }catch (Exception e) { e.printStackTrace(); } System.out.println("北京調(diào)度站車輛自檢完畢,一切正常"); } } |
package org.hzg.countDownLatch; import java.util.concurrent.CountDownLatch; /** * Created by hzgal on 2019-3-5. */ public class ShandongStation extends DangerCenter { private static String stationName = "山東危險(xiǎn)品調(diào)度站"; public ShandongStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("山東調(diào)度站正在進(jìn)行車輛自檢.........."); try { Thread.sleep(1000); }catch (Exception e) { e.printStackTrace(); } System.out.println("山東調(diào)度站車輛自檢完畢,一切正常"); } } |
package org.hzg.countDownLatch; import java.util.concurrent.CountDownLatch; /** * Created by hzgal on 2019-3-5. */ public class ShanxiStation extends DangerCenter { private static String stationName = "陜西危險(xiǎn)品調(diào)度站"; public ShanxiStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("陜西調(diào)度站正在進(jìn)行車輛自檢.........."); try { Thread.sleep(3000); }catch (Exception e) { e.printStackTrace(); } System.out.println("陜西調(diào)度站車輛自檢完畢,一切正常"); } } |
package org.hzg.countDownLatch; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; /** * Created by hzgal on 2019-3-5. */ public class Demo { public static void main(String[] args) { boolean isAllOk = true; List CountDownLatch countDownLatch = new CountDownLatch(3); stationList.add(new BeijingStation(countDownLatch)); stationList.add(new ShandongStation(countDownLatch)); stationList.add(new ShanxiStation(countDownLatch)); Executor stationThreadPool = Executors.newFixedThreadPool(3); for(DangerCenter stationCenter : stationList) { stationThreadPool.execute(stationCenter); } try { countDownLatch.await(); }catch (Exception e) { e.printStackTrace(); } for(DangerCenter stationCenter : stationList) { if(!stationCenter.isOkToSentCar()) { isAllOk = false; } } if (isAllOk) { System.out.println("所有站點(diǎn)檢查完畢,可以正常進(jìn)行發(fā)車?。。。。。。。。?); } } } |
程序運(yùn)行結(jié)果: 北京調(diào)度站正在進(jìn)行車輛自檢.......... 山東調(diào)度站正在進(jìn)行車輛自檢.......... 陜西調(diào)度站正在進(jìn)行車輛自檢.......... 山東調(diào)度站車輛自檢完畢,一切正常 北京調(diào)度站車輛自檢完畢,一切正常 陜西調(diào)度站車輛自檢完畢,一切正常 所有站點(diǎn)檢查完畢,可以正常進(jìn)行發(fā)車?。。。。。。。?! |