<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

    閱讀排行榜

    評論排行榜

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

    主站蜘蛛池模板: 最近中文字幕电影大全免费版| a级毛片高清免费视频就| 真人做人试看60分钟免费视频| 亚洲AV永久纯肉无码精品动漫| 日韩免费高清播放器| 亚洲嫩模在线观看| 中文字幕在线观看亚洲| 黄网站免费在线观看| 亚洲久本草在线中文字幕| 在线观看永久免费| 亚洲AV无码专区在线亚| 免费一本色道久久一区| 亚洲精品乱码久久久久久蜜桃图片 | 最新国产AV无码专区亚洲| 成人无码精品1区2区3区免费看| 0588影视手机免费看片| 亚洲一区中文字幕在线电影网| 在线永久免费的视频草莓| 中文字幕在线观看亚洲日韩| 性盈盈影院免费视频观看在线一区| 亚洲youwu永久无码精品| 免费二级毛片免费完整视频| 成人免费一区二区三区| 久久久婷婷五月亚洲97号色| 无码国产精品一区二区免费式直播| 亚洲国产高清国产拍精品| 亚洲 自拍 另类小说综合图区| 亚洲一区二区三区高清不卡| 永久免费视频v片www| 特级毛片aaaa免费观看| 亚洲视频在线播放| 日本特黄特色aa大片免费| selaoban在线视频免费精品| 亚洲欧洲日韩国产综合在线二区| 无码一区二区三区AV免费| 四虎影视久久久免费观看| 亚洲综合自拍成人| 免费人成在线观看播放国产| 爱丫爱丫影院在线观看免费| 国产亚洲精品成人AA片| 亚洲色无码一区二区三区|