淺析Java中CountDownLatch用法

/**
CountDownLatch類是一個同步計數器,構造時傳入int參數,該參數就是計數器的初始值,每調用一次countDown()方法,計數器減1,計數器大於0 時,await()方法會阻塞程序繼續執行
CountDownLatch如其所寫,是一個倒計數的鎖存器,當計數減至0時觸發特定的事件。利用這種特性,可以讓主線程等待子線程的結束。下面以一個模擬運動員比賽的例子加以說明。
*/
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.Executor;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
 
  public class CountDownLatchDemo {
     private static final int PLAYER_AMOUNT = 5;
     public CountDownLatchDemo() {
         // TODO Auto-generated constructor stub    
      }
     /**
      * @param args
      */
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         //對於每位運動員,CountDownLatch減1後即結束比賽
         CountDownLatch begin = new CountDownLatch(1);
         //對於整個比賽,所有運動員結束後纔算結束
         CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT);
         Player[] plays = new Player[PLAYER_AMOUNT];
         
         for(int i=0;i<PLAYER_AMOUNT;i++)
             plays[i] = new Player(i+1,begin,end);
         
         //設置特定的線程池,大小爲5
         ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
         for(Player p:plays)
             exe.execute(p);            //分配線程
         System.out.println("Race begins!");
         begin.countDown();
         try{
             end.await();            //等待end狀態變爲0,即爲比賽結束
         }catch (InterruptedException e) {
             // TODO: handle exception
             e.printStackTrace();
         }finally{
             System.out.println("Race ends!");
         }
         exe.shutdown();
     }
 }



import java.util.concurrent.CountDownLatch;
 
 
 public class Player implements Runnable {
 
     private int id;
     private CountDownLatch begin;
     private CountDownLatch end;
     public Player(int i, CountDownLatch begin, CountDownLatch end) {
         // TODO Auto-generated constructor stub
         super();
         this.id = i;
         this.begin = begin;
         this.end = end;
     }
 
     @Override
     public void run() {
         // TODO Auto-generated method stub
         try{
             begin.await();        //等待begin的狀態爲0
             Thread.sleep((long)(Math.random()*100));    //隨機分配時間,即運動員完成時間
             System.out.println("Play"+id+" arrived.");
         }catch (InterruptedException e) {
             // TODO: handle exception
             e.printStackTrace();
         }finally{
             end.countDown();    //使end狀態減1,最終減至0
         }
     }
 }

原文:http://www.cnblogs.com/yezhenhan/archive/2012/01/07/2315652.html

發佈了85 篇原創文章 · 獲贊 32 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章