標籤:java 同步 countdownlatch
在一些應用場合中,某段程式需要等待某個條件達到要求後才能執行,或者等待一定長的時間後此行,從jdk1.5開始就可以使用CountDownLatch實現,
CountDownLatch類是一個同步倒數計數器,構造時傳入int參數,該參數就是計數器的初始值,每調用一次countDown()方法,計數器減1,計數器大於0 時,await()方法會阻塞後面程式執行,直到計數器為0。
public class CountdownLatchTest {public static void main(String[] args) {ExecutorService service = Executors.newCachedThreadPool();final CountDownLatch cdOrder = new CountDownLatch(1);final CountDownLatch cdAnswer = new CountDownLatch(3);for(int i=0;i<3;i++){Runnable runnable = new Runnable(){public void run(){try {System.out.println("線程" + Thread.currentThread().getName() + "正準備接受命令");cdOrder.await();System.out.println("線程" + Thread.currentThread().getName() + "已接受命令");Thread.sleep((long)(Math.random()*10000));System.out.println("線程" + Thread.currentThread().getName() + "回應命令處理結果");cdAnswer.countDown();} catch (Exception e) {e.printStackTrace();}}};service.execute(runnable);}try {Thread.sleep((long)(Math.random()*10000));System.out.println("線程" + Thread.currentThread().getName() + "即將發布命令");cdOrder.countDown();System.out.println("線程" + Thread.currentThread().getName() + "已發送命令,正在等待結果");cdAnswer.await();System.out.println("線程" + Thread.currentThread().getName() + "已收到所有響應結果");} catch (Exception e) {e.printStackTrace();}service.shutdown();}}
上面樣本初始化兩個CountDownLatch樣本,分別傳入初始值1和3,cdOrder.await()後面的代碼,將會被阻塞,直到調用一次cdOrder.countDown()後才向下執行;cdAnswer.await()後面代碼阻塞,調用三次cdAnswer.countDown()向下執行。
所以上面執行結果:
線程pool-1-thread-1正準備接受命令
線程pool-1-thread-3正準備接受命令
線程pool-1-thread-2正準備接受命令
線程main即將發布命令
線程main已發送命令,正在等待結果
線程pool-1-thread-1已接受命令
線程pool-1-thread-2已接受命令
線程pool-1-thread-3已接受命令
線程pool-1-thread-2回應命令處理結果
線程pool-1-thread-3回應命令處理結果
線程pool-1-thread-1回應命令處理結果
線程main已收到所有響應結果
await(long timeout, TimeUnit unit),是等待一定時間,然後執行,不管計數器是否到0了。
下面例子來源:http://www.blogjava.net/freeman1984/archive/2011/07/04/353654.html
10個同學上車,車等待同學上車,如果有等待時間限制,到時間就開走,不管學生上沒上完。如果沒有等待時間,學生上完了再開:
public class CountDownLatchTest { public static int numberOfPeople = 10;//等車的學生數 public static boolean isGone = false;//車開的標誌 public static int carWaitTime = 3;//車等的時間 public static void main(String[] args) throws InterruptedException { CountDownLatch waitStudentsGetOn = new CountDownLatch(numberOfPeople); new Thread(new GetOn(waitStudentsGetOn)).start(); waitStudentGetOn(waitStudentsGetOn);//等所有的學生上車 driveHome();//開車走 } private static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException { System.out.println("趕緊的,抓緊時間上車.."); waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,還沒上車,就開走。。 } private static void driveHome() throws InterruptedException { System.out.println("開車,鞋兒破 帽兒破 身上的袈裟破 你笑我 他笑我 一把扇兒破"); isGone = true; } }class GetOn implements Runnable{ private CountDownLatch waitStudentsGetOn; GetOn(CountDownLatch waitStudentsGetOn){ this.waitStudentsGetOn = waitStudentsGetOn; } public void run() { for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) { try { if(CountDownLatchTest.isGone){ System.out.println("媽的,還差:"+waitStudentsGetOn.getCount()+" 個沒娃上車呢.怎麼車走了"); break; } boolean goonSuccess = new Student(i+1).getOn();//順序上車 if(goonSuccess)waitStudentsGetOn.countDown(); } catch (InterruptedException e) {} if(waitStudentsGetOn.getCount()!=0l){ System.out.println("還差:"+(waitStudentsGetOn.getCount())+" 個沒上車"); }else{ System.out.println("都上車了"); } } } class Student{ private int myNum;//學生編號 public Student(int num){ this.myNum = num; } //上車 public boolean getOn() throws InterruptedException{ Thread.currentThread().sleep(new Random().nextInt(2)*1000);//上車使用的時間,隨機 if(CountDownLatchTest.isGone){ return false;//不能上了,上車失敗 } System.out.print("編號為:"+myNum+"的同學上車了.."); return true; } }}
‘
java同步工具類之CountDownLatch