java並發之CountDownLatch、Semaphore和CyclicBarrier

來源:互聯網
上載者:User

標籤:

JAVA並發包中有三個類用於同步一批線程的行為,分別是CountDownLatch、Semaphore和CyclicBarrier。

CountDownLatch

CountDownLatch是一個計數器閉鎖,主要的功能就是通過await()方法來阻塞住當前線程,然後等待計數器減少到0了,再喚起這些線程繼續執行。 這個類裡主要有兩個方法,一個是向下減計數器的方法:countdown(),其實現的核心代碼如下:

public boolean tryReleaseShared(int releases) {  // Decrement count; signal when transition to zero  for (;;) {   int c = getState();     if (c == 0)    return false;     int nextc = c-1;    if (compareAndSetState(c, nextc))    return nextc == 0;    }    }    

很簡單,如果取得當前的狀態為0,說明這個鎖已經結束,直接返回false;如果沒有結束,然後去設定計數器減1,如果compareAndSetState不成功,則繼續迴圈執行。 而其中的一直等待計數器歸零的方法是await()。 
通過CountDownLatch可以做幾件事情:

1. 主線程式控制制同時啟動一組線程

final CountDownLatch count = new CountDownLatch(1);for (int i = 0; i < 3; i++) {    new Thread("Thread" + i) {        public void run() {            System.out.println(Thread.currentThread().getName() + " wait");            try {                count.await();            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + " start");        }    }.start();}//等等三秒,否則有可能3個線程並沒有全部進行await狀態try {    Thread.sleep(3000);} catch (InterruptedException e) {    e.printStackTrace();}count.countDown();

2. 主線程等待各子線程全部執行完畢後再往下執行:

final CountDownLatch count = new CountDownLatch(3);for (int i = 0; i < 3; i++) {    new Thread("Thread" + i) {        public void run() {            System.out.println(Thread.currentThread().getName() + " start");            count.countDown();        }    }.start();}try {    count.await();} catch (InterruptedException e) {    e.printStackTrace();}System.out.println("All end!!!");    
Semaphore

Semaphore與CountDownLatch相似,不同的地方在於Semaphore的值被擷取到後是可以釋放的,並不像CountDownLatch那樣一直減到底。它也被更多地用來限制流量,類似閥門的 功能。如果限定某些資源最多有N個線程可以訪問,那麼超過N個主不允許再有線程來訪問,同時當現有線程結束後,就會釋放,然後允許新的線程進來。有點類似於鎖的lock與 unlock過程。相對來說他也有兩個主要的方法:

  1. 用於擷取許可權的acquire(),其底層實現與CountDownLatch.countdown()類似;
  2. 用於釋放許可權的release(),其底層實現與acquire()是一個互逆的過程。

用Semaphore來實現限流代碼詳見:semaphore例子

CyclicBarrier

CyclicBarrier是用來一個關卡來阻擋住所有線程,等所有線程全部執行到關卡處時,再統一執行下一步操作,它裡面最重要的方法是await()方法,其實現如下:

private int dowait(boolean timed, long nanos)    throws InterruptedException, BrokenBarrierException,           TimeoutException {    //取鎖,以防止在後面做減1計數時線程不安全    final ReentrantLock lock = this.lock;    lock.lock();    try {        final Generation g = generation;        if (g.broken)            throw new BrokenBarrierException();        if (Thread.interrupted()) {            breakBarrier();            throw new InterruptedException();        }       //如果當前線程執行到了,則將計數器減1,計數器為0則說明所有線程均執行到這裡,可以調用下一步操作       int index = --count;       if (index == 0) {  // tripped           boolean ranAction = false;           try {               //擷取到定義好的下一步操作,並執行   final Runnable command = barrierCommand;               if (command != null)                   command.run();               ranAction = true;               nextGeneration();               return 0;           } finally {               if (!ranAction)                   breakBarrier();           }       }        // loop until tripped, broken, interrupted, or timed out        for (;;) {            try {                if (!timed)                    trip.await();                else if (nanos > 0L)                    nanos = trip.awaitNanos(nanos);            } catch (InterruptedException ie) {                if (g == generation && ! g.broken) {                    breakBarrier();throw ie;    } else {// We‘re about to finish waiting even if we had not// been interrupted, so this interrupt is deemed to// "belong" to subsequent execution.Thread.currentThread().interrupt();    }            }            if (g.broken)                throw new BrokenBarrierException();            if (g != generation)                return index;            if (timed && nanos <= 0L) {                breakBarrier();                throw new TimeoutException();            }        }    } finally {        lock.unlock();    }}    

即每個線程執行完後調用await(),然後在await()裡,線程先將計數器減1,如果計數器為0,則執行定義好的操作,然後再繼續執行原線程的內容。 
這個類比之前兩個類的一個好處是有點類似於切面編程,可以讓我們在同類線程的某個切面切入一塊邏輯,並且可以同步所有的線程的執行速度。
例子代碼如下:

final CyclicBarrier barrier = new CyclicBarrier(4, new Runnable() {    @Override    public void run() {        System.out.println("All Threads Here");    }});for (int i = 0; i < 4; i++) {    new Thread("Thread" + i) {        public void run() {            System.out.println(Thread.currentThread().getName() + " wait");            try {                barrier.await();            } catch (InterruptedException e) {                e.printStackTrace();            } catch (BrokenBarrierException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + " crossed");        }    }.start();}  

最終的輸出結果為:

Thread0 wait
Thread1 wait 
Thread2 wait 
Thread3 wait 
All Threads Here 
Thread0 crossed 
Thread1 crossed 
Thread2 crossed 
Thread3 crossed

java並發之CountDownLatch、Semaphore和CyclicBarrier

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.