1. 程式人生 > >java執行緒中鎖存器CountDownLatch的使用

java執行緒中鎖存器CountDownLatch的使用

CountDownLatch類是一個同步計數器,構造時傳入int引數,該引數就是計數器的初始值,每呼叫一次countDown()方法,計數器減1,計數器大於0 時,await()方法會阻塞程式繼續執行。CountDownLatch可以看作是一個倒計數的鎖存器,當計數減至0時觸發特定的事件。利用這種特性,可以讓主執行緒等待子執行緒的結束。下面以一個模擬運動員比賽的例子加以說明。

 CountDownLatch的一個非常典型的應用場景是:有一個任務想要往下執行,但必須要等到其他的任務執行完畢後才可以繼續往下執行。假如我們這個想要繼續往下執行的任務呼叫一個CountDownLatch物件的await()方法,其他的任務執行完自己的任務後呼叫同一個CountDownLatch物件上的countDown()方法,這個呼叫await()方法的任務將一直阻塞等待,直到這個CountDownLatch物件的計數值減到0為止。

1 import java.util.concurrent.CountDownLatch;
2 import java.util.concurrent.Executor;
3 import java.util.concurrent.ExecutorService;
4 import java.util.concurrent.Executors;
5 6 publicclass CountDownLatchDemo {
7 privatestaticfinalint PLAYER_AMOUNT =5;
8 public CountDownLatchDemo() {
9 // TODO Auto-generated constructor stub 
10  }
11 /**12 @param args
13 */14 publicstaticvoid main(String[] args) {
15 // TODO Auto-generated method stub
16 //對於每位運動員,CountDownLatch減1後即結束比賽17 CountDownLatch begin =new CountDownLatch(1);
18 //對於整個比賽,所有運動員結束後才算結束19 CountDownLatch end =new CountDownLatch(PLAYER_AMOUNT);
20 Player[] plays =new Player[PLAYER_AMOUNT];
21 22 for(int i=0;i<PLAYER_AMOUNT;i++)
23 plays[i] =new Player(i+1,begin,end);
24 25 //設定特定的執行緒池,大小為526 ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
27 for(Player p:plays)
28 exe.execute(p); //分配執行緒29 System.out.println("Race begins!");
30 begin.countDown();
31 try{
32 end.wait(); //等待end狀態變為0,即為比賽結束33 }catch (InterruptedException e) {
34 // TODO: handle exception35 e.printStackTrace();
36 }finally{
37 System.out.println("Race ends!");
38 }
39 exe.shutdown();
40 }
41 }
複製程式碼

接下來是Player類

注:countDown最好是在finally裡面呼叫

複製程式碼 1 import java.util.concurrent.CountDownLatch;
2 3 4 publicclass Player implements Runnable {
5 6 privateint id;
7 private CountDownLatch begin;
8 private CountDownLatch end;
9 public Player(int i, CountDownLatch begin, CountDownLatch end) {
10 // TODO Auto-generated constructor stub11 super();
12 this.id = i;
13 this.begin = begin;
14 this.end = end;
15 }
16 17 @Override
18 publicvoid run() {
19 // TODO Auto-generated method stub20 try{
21 begin.await(); //等待begin的狀態為022 Thread.sleep((long)(Math.random()*100)); //隨機分配時間,即運動員完成時間23 System.out.println("Play"+id+" arrived.");
24 }catch (InterruptedException e) {
25 // TODO: handle exception26 e.printStackTrace();
27 }finally{
28 end.countDown(); //使end狀態減1,最終減至029 }
30 }
31 } 複製程式碼

下面是張孝祥老師所舉例子的程式程式碼和執行結果:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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 (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   };
   service.execute(runnable);
  }
  try {
   System.out.println("執行緒"+Thread.currentThread().getName()+"即將釋出命令");
   cdOrder.countDown();
   System.out.println("執行緒"+Thread.currentThread().getName()+"已接受命令,正在等待結果");
   cdAnswer.await();
   Thread.sleep((long)(Math.random()*10000));
   System.out.println("執行緒"+Thread.currentThread().getName()+"已收到所有響應結果");
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  service.shutdown();
 }
}
執行結果:

執行緒pool-1-thread-1正準備接受命令
執行緒pool-1-thread-2正準備接受命令
執行緒pool-1-thread-3正準備接受命令
執行緒main即將釋出命令
執行緒main已接受命令,正在等待結果
執行緒pool-1-thread-2已接受命令
執行緒pool-1-thread-1已接受命令
執行緒pool-1-thread-3已接受命令
執行緒pool-1-thread-2迴應命令處理結果
執行緒pool-1-thread-1迴應命令處理結果
執行緒pool-1-thread-3迴應命令處理結果
執行緒main已收到所有響應結果