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

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

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

    統(tǒng)計(jì)

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評(píng)論排行榜

    CountDownLatch in Java

      
     1. Description
           A java.util.concurrent.CountDownLatch is a concurrency construct that allows one or more threads to wait for a given set of operations to complete.
           A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times

    3. Its methods
    void await()
    Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.
    boolean await(long timeout, TimeUnit unit)
    Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted, or the specified waiting time elapses.
    void countDown()
    Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
    long getCount()
    Returns the current count.
    String toString()
    Returns a string identifying this latch, as well as its state.


    3.Sample usage:
         *The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed;
         *The second is a completion signal that allows the driver to wait until all workers have completed.

    class Driver {
        
    public static void main(String[] args) throws InterruptedException {
            
    final int N = 3;
            CountDownLatch startSignal 
    = new CountDownLatch(1);
            CountDownLatch doneSignal 
    = new CountDownLatch(N);

            
    for (int i = 0; i < N; ++i)
                
    new Thread(new Worker(startSignal, doneSignal)).start(); // create and start threads

            doSomethingElse(); 
    // don't let run yet
            startSignal.countDown(); // let all threads proceed
            doSomethingElse();
            doneSignal.await(); 
    // wait for all to finish
            System.out.println("Diver has waited for all workers\' work completed");
        }


        
    private static void doSomethingElse() {
                System.out.println(
    "Driver is doing sth. else");
        }

    }


    class Worker implements Runnable {
        
    private final CountDownLatch startSignal;
        
    private final CountDownLatch doneSignal;

        Worker(CountDownLatch startSignal, CountDownLatch doneSignal) 
    {
            
    this.startSignal = startSignal;
            
    this.doneSignal = doneSignal;
        }


        
    public void run() {
            
    try {
                startSignal.await();
                doWork();
                doneSignal.countDown();
                System.out.println(
    "done signal, count is " + doneSignal.getCount());
            }
     catch (InterruptedException ex) {
            }
     // return;
        }


        
    private void doWork() {
            System.out.println(
    "Worker is doing his work");
        }

    }

    4. Reference
        api:http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html

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

    主站蜘蛛池模板: 亚洲国产成人精品久久久国产成人一区二区三区综 | 精品特级一级毛片免费观看| 麻豆精品国产免费观看| 理论秋霞在线看免费| 亚洲一区二区三区偷拍女厕| 国产高清不卡免费视频| 91在线亚洲综合在线| 亚洲一区无码精品色| 国产精品亚洲二区在线观看 | 亚洲精品国产免费| 最新亚洲人成无码网www电影| 亚洲第一区精品日韩在线播放| 99精品视频免费观看| 亚洲一区二区三区久久久久| 国产精品成人观看视频免费| 极品色天使在线婷婷天堂亚洲| 亚洲婷婷五月综合狠狠爱| 日本XXX黄区免费看| 中文字幕久无码免费久久| 亚洲人成人网站18禁| 亚洲激情视频在线观看| 免费a级毛片网站| 999久久久免费精品国产| 亚洲精品视频免费观看| 亚洲日韩AV一区二区三区中文 | 男女免费观看在线爽爽爽视频| 在线观看免费成人| 国产好大好硬好爽免费不卡| 在线亚洲v日韩v| 2020国产精品亚洲综合网 | 亚洲 欧洲 自拍 另类 校园| 亚洲国产精品久久久天堂| 精品无码国产污污污免费| 57pao一国产成永久免费| 成人无码WWW免费视频| 九九久久精品国产免费看小说| 亚洲色最新高清av网站| 国产亚洲精品成人AA片| avtt天堂网手机版亚洲| 亚洲H在线播放在线观看H| 亚洲国产日韩在线成人蜜芽|