隨筆 - 0, 文章 - 264, 評論 - 170, 引用 - 0
          數據加載中……

          CountDownLatch的使用(注:轉載于http://www.aygfsteel.com/fanjs2000/archive/2012/10/31/390521.html)

          CountDownLatch如其所寫,是一個倒計數的鎖存器,當計數減至0時觸發特定的事件。利用這種特性,可以讓主線程等待子線程的結束。下面以一個模擬運動員比賽的例子加以說明。

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

          public class Test {
              
          private static final int PLAYER_AMOUNT = 5;

              
          public static void main(String[] args) {        
                  
          //對于每位運動員,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); 
          //分配線程
                  begin.countDown();
                  System.out.println(
          "Race begins!");
                  
          try {
                      end.await(); 
          //等待end狀態變為0,即為比賽結束
                  } catch (InterruptedException e) {
                      
          // TODO: handle exception
                      e.printStackTrace();
                  } 
          finally {
                      System.out.println(
          "Race ends!");
                  }
                  exe.shutdown();
              }
          }

          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;
              }

              
          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
                  }
              }
          }

          posted on 2012-10-31 17:19 小一敗涂地 閱讀(489) 評論(0)  編輯  收藏 所屬分類: 并發、多線程java語言相關

          主站蜘蛛池模板: 伊宁县| 西平县| 垣曲县| 玉林市| 阳曲县| 高尔夫| 景宁| 方城县| 宁南县| 嘉鱼县| 高安市| 神农架林区| 若尔盖县| 安化县| 本溪市| 三原县| 肥东县| 成安县| 通海县| 新巴尔虎右旗| 兴安盟| 合阳县| 四子王旗| 花莲县| 信宜市| 论坛| 陈巴尔虎旗| 赤壁市| 剑河县| 定襄县| 鄂尔多斯市| 宁波市| 龙山县| 余庆县| 乐山市| 揭西县| 灌云县| 天镇县| 张掖市| 略阳县| 德格县|