<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    統計

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評論排行榜

    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 閱讀(645) 評論(0)  編輯  收藏 所屬分類: Programing

    主站蜘蛛池模板: 亚洲视频在线一区二区| 亚洲综合校园春色| 麻豆视频免费观看| 亚洲aⅴ天堂av天堂无码麻豆 | 国产免费卡一卡三卡乱码| 特黄特色大片免费| 亚洲色大成网站www永久| 免费羞羞视频网站| 黄色片免费在线观看| 亚洲AV成人无码网站| 亚洲2022国产成人精品无码区| 成熟女人牲交片免费观看视频| 国产黄在线播放免费观看| 亚洲av无码久久忘忧草| 亚洲天堂在线视频| 成年在线观看免费人视频草莓| 中文在线观看国语高清免费| 亚洲人成电影在线观看青青| 亚洲日韩精品无码一区二区三区 | 亚洲精品乱码久久久久久蜜桃| 99精品视频在线观看免费专区| 国产亚洲漂亮白嫩美女在线 | 国产一级片免费看| 欧洲亚洲综合一区二区三区 | 免费精品无码AV片在线观看| 精品亚洲成a人在线观看| 久久亚洲精品成人无码网站| 免费一级一片一毛片| 久九九精品免费视频| 黄色网站软件app在线观看免费 | 日韩人妻一区二区三区免费| 一级特黄色毛片免费看| 亚洲日韩国产欧美一区二区三区| 亚洲av鲁丝一区二区三区| 亚洲一级片免费看| 国产精品免费综合一区视频| 免费看美女裸露无档网站| 黄网站免费在线观看| 一级**爱片免费视频| 国产成人高清亚洲一区久久 | 亚洲精品午夜在线观看|