統(tǒng)計(jì)

          留言簿(1)

          DB

          Others

          QA

          Tech Website

          閱讀排行榜

          評(píng)論排行榜

          CyclicBarrier in Java

          1. Description
             A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.


          The threads wait for each other by calling the await() method on the CyclicBarrier. Once N threads are waiting at the CyclicBarrier, all threads are released and can continue running

          2. API
          int await()
          Waits until all parties have invoked await on this barrier.
          int await(long timeout, TimeUnit unit)
          Waits until all parties have invoked await on this barrier.
          int getNumberWaiting()
          Returns the number of parties currently waiting at the barrier.
          int getParties()
          Returns the number of parties required to trip this barrier.
          boolean isBroken()
          Queries if this barrier is in a broken state.
          void reset()
          Resets the barrier to its initial state.


          3. Sample
          public class CyclicBarrierRunnable implements Runnable{

              CyclicBarrier barrier1 
          = null;
              CyclicBarrier barrier2 
          = null;

              
          public CyclicBarrierRunnable(
                      CyclicBarrier barrier1,
                      CyclicBarrier barrier2) 
          {

                  
          this.barrier1 = barrier1;
                  
          this.barrier2 = barrier2;
              }


              
          public void run() {
                  
          try {
                      Thread.sleep(
          1000);
                      System.out.println(Thread.currentThread().getName() 
          +
                                          
          " waiting at barrier 1");
                      
          this.barrier1.await();

                      Thread.sleep(
          1000);
                      System.out.println(Thread.currentThread().getName() 
          +
                                          
          " waiting at barrier 2");
                      
          this.barrier2.await();

                      System.out.println(Thread.currentThread().getName() 
          +
                                          
          " done!");

                  }
           catch (InterruptedException e) {
                      e.printStackTrace();
                  }
           catch (BrokenBarrierException e) {
                      e.printStackTrace();
                  }

              }

          }



          public class CyclicBarrierMain {
              
              
          public static void main(String[] args){
                  Runnable barrier1Action 
          = new Runnable() {
                      
          public void run() {
                          System.out.println(
          "BarrierAction 1 executed ");
                      }

                  }
          ;
                  Runnable barrier2Action 
          = new Runnable() {
                      
          public void run() {
                          System.out.println(
          "BarrierAction 2 executed ");
                      }

                  }
          ;

                  CyclicBarrier barrier1 
          = new CyclicBarrier(2, barrier1Action);
                  CyclicBarrier barrier2 
          = new CyclicBarrier(2, barrier2Action);

                  CyclicBarrierRunnable barrierRunnable1 
          =
                          
          new CyclicBarrierRunnable(barrier1, barrier2);

                  CyclicBarrierRunnable barrierRunnable2 
          =
                          
          new CyclicBarrierRunnable(barrier1, barrier2);

                  
          new Thread(barrierRunnable1).start();
                  
          new Thread(barrierRunnable2).start();

              }

              Runnable barrier1Action 
          = new Runnable() {
                  
          public void run() {
                      System.out.println(
          "BarrierAction 1 executed ");
                  }

              }
          ;
              Runnable barrier2Action 
          = new Runnable() {
                  
          public void run() {
                      System.out.println(
          "BarrierAction 2 executed ");
                  }

              }
          ;
          }

          posted on 2011-05-07 15:35 XXXXXX 閱讀(648) 評(píng)論(0)  編輯  收藏 所屬分類: Programing

          主站蜘蛛池模板: 登封市| 栾城县| 大庆市| 大厂| 营口市| 毕节市| 平武县| 阿合奇县| 盐边县| 平定县| 永丰县| 兰州市| 库尔勒市| 永吉县| 青岛市| 临朐县| 夹江县| 大冶市| 仙桃市| 天长市| 盐城市| 临西县| 汉阴县| 昭平县| 延吉市| 邮箱| 北流市| 临泉县| 平罗县| 普陀区| 南溪县| 星座| 修武县| 石河子市| 富阳市| 新密市| 宣汉县| 甘肃省| 噶尔县| 历史| 苗栗市|