解讀java同步類CountDownLatch

來源:互聯網
上載者:User

標籤:hub   releases   thread   數值   release   nano   ansi   runnable   工程   

同步輔助類:

    CountDownLatch是一個同步輔助類,在jdk5中引入,它允許一個或多個線程等待其他線程操作完成之後才執行。    

實現原理 :

    CountDownLatch是通過計數器的方式來實現,計數器的初始值為線程的數量。每當一個線程完成了自己的任務之後,就會對計數器減1,當計數器的值為0時,表示所有線程完成了任務,此時等待在閉鎖上的線程才繼續執行,從而達到等待其他線程完成任務之後才繼續執行的目的。

 

CountDownLatch主要方法:

    CountDownLatch具體是通過同步器來實現的,使用AQS狀態來表示計數:

    /**     * Synchronization control For CountDownLatch.     * Uses AQS state to represent count.     */    private static final class Sync extends AbstractQueuedSynchronizer {        private static final long serialVersionUID = 4982264981922014374L;        Sync(int count) {            setState(count);        }        int getCount() {            return getState();        }        protected int tryAcquireShared(int acquires) {            return (getState() == 0) ? 1 : -1;        }        protected 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;            }        }    }    private final Sync sync;

  

1、建構函式 

    public CountDownLatch(int count) {        if (count < 0) throw new IllegalArgumentException("count < 0");        this.sync = new Sync(count);    }

通過傳入一個數值來建立一個CountDownLatch,數值表示線程可以從等待狀態恢複,countDown方法必須被調用的次數

 

2、countDown方法

public void countDown() {        sync.releaseShared(1);    }  

線程調用此方法對count進行減1。當count本來就為0,此方法不做任何操作,當count比0大,調用此方法進行減1,當new count為0,釋放所有等待當線程。

 

3、await方法

(1)不帶參數

    public void await() throws InterruptedException {        sync.acquireSharedInterruptibly(1);    }

調用此方法時,當count為0,直接返回true,當count比0大,線程會一直等待,直到count的值變為0,或者線程被中斷(interepted,此時會拋出中斷異常)。

(2)帶參數

    public boolean await(long timeout, TimeUnit unit)        throws InterruptedException {        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));    }

調用此方法時,當count為0,直接返回true,當count比0大,線程會等待一段時間,等待時間內如果count的值變為0,返回true;當超出等待時間,返回false;或者等待時間內線程被中斷,此時會拋出中斷異常。 

 

CountDownLatch實踐

司機和工人,工人必須等到司機來了才能裝貨上車,司機必須得等到所有工人把貨物裝上車了之後才能把車開走。

工人類:

public class Worker implements Runnable {    private String workerCode;    private CountDownLatch startLatch;    private CountDownLatch latch;    Worker(CountDownLatch startLatch, CountDownLatch latch, String workerCode) {        this.startLatch = startLatch;        this.latch = latch;        this.workerCode = workerCode;    }    public void run() {        try {            startLatch.await();            doWork();            latch.countDown();        } catch (InterruptedException e) {            e.printStackTrace();        }    }    private void doWork() {        System.out.println("Worker " + workerCode + " is loading goods...");    }}

司機類:

public class Driver {    public static void main(String[] args) throws InterruptedException {        CountDownLatch startLatch = new CountDownLatch(1);        CountDownLatch latch = new CountDownLatch(10);        ExecutorService executor = Executors.newFixedThreadPool(10);        for(int i=0; i<10; i++) {            executor.execute(new Worker(startLatch, latch, "worker" + i));        }        System.out.println("Driver is here.");        startLatch.countDown();        System.out.println("Workers get to work.");        latch.await();        System.out.println("Driver is ready to go.");        executor.shutdown();    }}  

運行結果:

Driver is here.Workers get to work.Worker worker0 is loading goods...Worker worker1 is loading goods...Worker worker2 is loading goods...Worker worker3 is loading goods...Worker worker4 is loading goods...Worker worker5 is loading goods...Worker worker7 is loading goods...Worker worker9 is loading goods...Worker worker8 is loading goods...Worker worker6 is loading goods...Driver is ready to go.

 

完整工程代碼請參考git:https://github.com/xuweijian/CountDownLatch.git

 

解讀java同步類CountDownLatch

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.