【JAVA】java中CountDownLatch的用法,執行個體講解

來源:互聯網
上載者:User

標籤:


CountDownLatch主要用於多線程環境中,當所有的線程都countDown了,就會釋放所有的等待的線程,await在到0之前一直等待。

直接看一段代碼:

package thread.thread;import java.util.concurrent.CountDownLatch;import java.util.concurrent.LinkedBlockingDeque;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;/** * ClassName:CountDownLatchTest <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2015年7月30日 下午2:04:07 <br/> *  * @author chiwei * @version * @since JDK 1.6 * @see */public class CountDownLatchTest {public static void main(String[] args) {ThreadPoolExecutor poolExe = new ThreadPoolExecutor(100, 1000, 1, TimeUnit.SECONDS,    new LinkedBlockingDeque<Runnable>(100));// 考試開始鈴聲響起,考試開始final CountDownLatch examBegin = new CountDownLatch(1);// 單個考生,考試結束交卷final CountDownLatch student = new CountDownLatch(10);// 一個考場10位考生for (int i = 0; i < 10; i++) {Runnable runnable = new Runnable() {public void run() {try {System.out.println("考生" + Thread.currentThread().getName() + "在等待考試開始的鈴聲響起");examBegin.await();System.out.println("考生聽到鈴聲" + Thread.currentThread().getName() + "開始答題");Thread.sleep((long) (Math.random() * 100));//答題過程,真正的商務邏輯處理部分System.out.println("考生" + Thread.currentThread().getName() + "交卷");student.countDown();} catch (Exception e) {e.printStackTrace();}}};poolExe.execute(runnable); // 運動員開始任務}try {// 答題時間Thread.sleep((long) (Math.random() * 10000));System.out.println("考場" + Thread.currentThread().getName() + "開始鈴聲即將響起");examBegin.countDown(); // 命令計數器置為0System.out.println("考場" + Thread.currentThread().getName() + "考試開始鈴聲響起");student.await(); // 所有考生交卷System.out.println("考場" + Thread.currentThread().getName() + "考試結束");} catch (Exception e) {e.printStackTrace();}poolExe.shutdown();}}

如以上代碼所示,有兩個CountDownLatch類,一個控制考場,整個考試的開始和結束,一個控制所有考生是否都交卷了。

examBegin只有在countDown後,10個考生才有可能開始答題,因為答題前有examBegin.await(),會阻塞住;同理,在考試結束前,student.await()會阻塞住,因為只有當所有的學生都交卷了(countDown),await才會通過。

考生pool-1-thread-3在等待考試開始的鈴聲響起考生pool-1-thread-2在等待考試開始的鈴聲響起考生pool-1-thread-7在等待考試開始的鈴聲響起考生pool-1-thread-10在等待考試開始的鈴聲響起考生pool-1-thread-8在等待考試開始的鈴聲響起考生pool-1-thread-1在等待考試開始的鈴聲響起考生pool-1-thread-6在等待考試開始的鈴聲響起考生pool-1-thread-9在等待考試開始的鈴聲響起考生pool-1-thread-4在等待考試開始的鈴聲響起考生pool-1-thread-5在等待考試開始的鈴聲響起考場main開始鈴聲即將響起考場main考試開始鈴聲響起考生聽到鈴聲pool-1-thread-3開始答題考生聽到鈴聲pool-1-thread-10開始答題考生聽到鈴聲pool-1-thread-7開始答題考生聽到鈴聲pool-1-thread-2開始答題考生聽到鈴聲pool-1-thread-8開始答題考生聽到鈴聲pool-1-thread-6開始答題考生聽到鈴聲pool-1-thread-9開始答題考生聽到鈴聲pool-1-thread-1開始答題考生聽到鈴聲pool-1-thread-4開始答題考生聽到鈴聲pool-1-thread-5開始答題考生pool-1-thread-4交卷考生pool-1-thread-8交卷考生pool-1-thread-9交卷考生pool-1-thread-1交卷考生pool-1-thread-10交卷考生pool-1-thread-3交卷考生pool-1-thread-2交卷考生pool-1-thread-6交卷考生pool-1-thread-5交卷考生pool-1-thread-7交卷考場main考試結束
多線程使用情境下,你的CountDownLatch的count值要等於線程數。

等待count個線程全部執行完了,整個任務才算執行結束了。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

【JAVA】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.