Usage of Countdownlatch in Java Java, example explained

Source: Internet
Author: User


Countdownlatch is mainly used in multi-threaded environments, when all threads are countdown, all waiting threads are released, and await waits until 0.

Look directly at the code:

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 July 30 pm 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));//The exam starts ringing, the exam begins final Countdownlatch exambegin = n EW Countdownlatch (1);//single examinee, final exam completed countdownlatch student = new Countdownlatch (10);//One Examination Room 10 candidates for (int i = 0; I &lt ; 10; i++) {Runnable Runnable = new Runnable () {public void run () {try {System.out.println ("candidate" + Thread.CurrentThread (). GetName () + "bell ringing waiting for the test to start"); Exambegin.await (); SYSTEM.OUT.PRINTLN ("Candidates hear ringtones" + THread.currentthread (). GetName () + "start Answer"); Thread.Sleep ((Long) (Math.random () * 100));//answer process, real business logic processing part SYSTEM.OUT.PRINTLN ("examinee" + thread.currentthread (). GetName () + "handing in"); Student.countdown ();} catch (Exception e) {e.printstacktrace ();}}}; Poolexe.execute (runnable); Athlete starts task}try {//Answer time Thread.Sleep ((Long) (Math.random () * 10000)); SYSTEM.OUT.PRINTLN ("Examination room" + thread.currentthread (). GetName () + "Start ringtone is about to ring"); Exambegin.countdown (); The command counter is set to 0SYSTEM.OUT.PRINTLN ("Examination room" + thread.currentthread (). GetName () + "Test starts ringing"); student.await (); All candidates are handed in System.out.println ("Examination room" + thread.currentthread (). GetName () + "End of Exam");} catch (Exception e) {e.printstacktrace ();} Poolexe.shutdown ();}}

As shown in the above code, there are two countdownlatch classes, a control examination room, the beginning and end of the entire test, a control whether all candidates are handed in.

Exambegin only after the countdown, 10 candidates can start the answer, because there is exambegin.await () before the answer, will block, similarly, before the end of the exam, student.await () will block, Because only when all the students have handed in their papers (countdown), the await will pass.

Examinee pool-1-thread-3 waiting for the test to start the bell sounded examinee pool-1-thread-2 waiting for the test to start the bell sounded examinee pool-1-thread-7 waiting for the test to start the bell sounded examinee pool-1-thread-10 waiting for the test to start the bell sounded examinee PO Ol-1-thread-8 waiting for the test to start the bell sounded examinee pool-1-thread-1 waiting for the test to start the bell sounded examinee pool-1-thread-6 waiting for the test to start the bell sounded examinee pool-1-thread-9 waiting for the test to start the bell sounded examinee pool-1- Thread-4 waiting for the test to start the bell rang test taker pool-1-thread-5 waiting for the test to start the bell rang the examination room main starts ringing the examination room main test start bell sounded examinee hear ringtone pool-1-thread-3 start answer test taker hear ringtone pool-1-thread -10 start answer the test taker hears the ringtone pool-1-thread-7 begins to answer the test taker hears the bell pool-1-thread-2 starts to answer the test taker hears the ringtone pool-1-thread-8 starts to answer the test taker hears the ringtone pool-1-thread-6 begins to answer the Examinee hears the ringtone Hread-9 begin to answer the test taker hears the Bell pool-1-thread-1 start answering the test taker hears the bell pool-1-thread-4 starts to answer the examinee hears the ringtone pool-1-thread-5 starts to answer the examinee pool-1-thread-4 to test the examinee pool-1-thread- 8 to test the examinee pool-1-thread-9 to test the examinee pool-1-thread-1 to test the examinee pool-1-thread-10 handed in the examinee pool-1-thread-3 handed in the examinee pool-1-thread-2 test taker pool-1-thread-6 test taker P Ool-1-thread-5 test Taker POOL-1-THREAD-7 Examination exam completed main exams
The count value of your countdownlatch is equal to the number of threads in a multithreaded usage scenario.

Wait until count threads are all done, and the entire task is finished.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Usage of Countdownlatch in Java Java, example explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.