CountDownLatch introduction and Examples

Source: Internet
Author: User

In some application scenarios, a program can be executed only after a certain condition is met, or after a certain period of time, CountDownLatch can be used from jdk1.5,
The CountDownLatch class is a synchronous countDown counter. When constructing a counter, an int parameter is input. This parameter is the initial value of the counter. Every time the countDown () method is called, the counter minus 1. When the counter is greater than 0, the await () method will block subsequent program execution until the counter is 0, await (long timeout, TimeUnit unit), wait for a certain period of time, and then execute, regardless of whether the counter reaches 0.
The following is an example of waiting for a car:
Ten students get on the bus and the car waits for the students to get on the bus. If there is a waiting time limit, they will leave when the time is reached, regardless of whether the students are not finished. If there is no waiting time, students are finished and then open: public class CountDownLatchTest {
Public static int numberOfPeople = 10; // Number of students waiting for a bus
Public static boolean isGone = false; // Vehicle Driving flag
Public static int carWaitTime = 3; // vehicle time

Public static void main (String [] args) throws InterruptedException {

CountDownLatch waitStudentsGetOn = new CountDownLatch (numberOfPeople );

New Thread (new GetOn (waitStudentsGetOn). start ();

WaitStudentGetOn (waitStudentsGetOn); // wait for all students to get on the bus.

DriveHome (); // drive

}

Private static void waitStudentGetOn (CountDownLatch waitStudentsGetOn) throws InterruptedException {
System. out. println ("Hurry up, get on the bus ..");
WaitStudentsGetOn. await (carWaitTime, TimeUnit. SECONDS); // wait for 5 SECONDS until you get on the bus ..

}

Private static void driveHome () throws InterruptedException {
System. out. println ");
IsGone = true;

}

}
Class GetOn implements Runnable {

Private CountDownLatch waitStudentsGetOn;
GetOn (CountDownLatch waitStudentsGetOn ){
This. waitStudentsGetOn = waitStudentsGetOn;
}
Public void run (){
For (int I = 0; I <CountDownLatchTest. numberOfPeople; I ++ ){
Try {
If (CountDownLatchTest. isGone ){
System. out. println ("Mom, it's still bad:" + waitStudentsGetOn. getCount () + "How can I get on the bus? How can I get on the bus ");
Break;
}
Boolean goonSuccess = new Student (I + 1). getOn (); // get in order
If (goonSuccess) waitStudentsGetOn. countDown ();
} Catch (InterruptedException e ){}
If (waitStudentsGetOn. getCount ()! = 0l ){
System. out. println ("poor:" + (waitStudentsGetOn. getCount () + "no bus ");
} Else {
System. out. println ("all on the bus ");
}
}


}
Class Student {
Private int myNum; // student ID
Public Student (int num ){
This. myNum = num;
}
// Get on the bus
Public boolean getOn () throws InterruptedException {
Thread. currentThread (). sleep (new Random (). nextInt (2) * 1000); // time used for boarding, Random
If (CountDownLatchTest. isGone ){
Return false; // failed to get on the bus
}
System. out. print ("" + myNum + ..");
Return true;
}
}
}

Related Article

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.