您好,登錄后才能下訂單哦!
CountDownLatch在java.util.concurrent.CountDownLatch包里面,他通常用在多線程中,假如說我們現在有一件事情開了多線程去完成,但是我們需要保證所有的線程都成功運行結束,再去執行后面的代碼,這個時候CountDownLatch這個類就派上用場了。
我們可以把CountDownLatch傳入到每個線程里面去,在線程執行成功時調用countDownLatch.countDown();方法,然后在主線程里面執行完多線程初始化和分發執行之后執行countDownLatch.await();這樣,只有在每個線程執行成功以后,也就是CountDownLatch的計數器減到0以后,才會執行countDownLatch.await()后面的代碼。
簡單的應用場景是這樣的,但是具體的怎么設計還是需要仔細斟酌,否則有可能會出現countDownLatch.countDown();永遠無法減為0的情況。
下面一個簡單的例子來實現這一應用場景:
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; //計數器 private String station; //危險品調度站 private boolean isOkToSentCar; //檢查結果,是否滿足發車條件 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方法,用于實現了DangerCenter的各級調度中心來重寫check方法,因為每個調度中心自檢的方法可能不一樣 */ 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 = "北京危險品調度站"; public BeijingStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("北京調度站正在進行車輛自檢.........."); try { Thread.sleep(2000); }catch (Exception e) { e.printStackTrace(); } System.out.println("北京調度站車輛自檢完畢,一切正常"); } } |
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 = "山東危險品調度站"; public ShandongStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("山東調度站正在進行車輛自檢.........."); try { Thread.sleep(1000); }catch (Exception e) { e.printStackTrace(); } System.out.println("山東調度站車輛自檢完畢,一切正常"); } } |
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 = "陜西危險品調度站"; public ShanxiStation(CountDownLatch countDownLatch) { super(countDownLatch,stationName); } @Override public void check() { System.out.println("陜西調度站正在進行車輛自檢.........."); try { Thread.sleep(3000); }catch (Exception e) { e.printStackTrace(); } System.out.println("陜西調度站車輛自檢完畢,一切正常"); } } |
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<DangerCenter> stationList = new ArrayList<DangerCenter>(); 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("所有站點檢查完畢,可以正常進行發車!!!!!!!!!"); } } } |
程序運行結果: 北京調度站正在進行車輛自檢.......... 山東調度站正在進行車輛自檢.......... 陜西調度站正在進行車輛自檢.......... 山東調度站車輛自檢完畢,一切正常 北京調度站車輛自檢完畢,一切正常 陜西調度站車輛自檢完畢,一切正常 所有站點檢查完畢,可以正常進行發車!!!!!!!!! |
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。