In some applications, a program needs to wait for a certain condition to meet the requirements before execution, or wait a long time after the trip, starting from jdk1.5 can use the Countdownlatch implementation,
The Countdownlatch class is a synchronous reciprocal counter, constructed with an int parameter, which is the initial value of the counter, each time the countdown () method is called, the counter is minus 1, the counter is greater than 0 o'clock, and the await () method blocks the execution of the subsequent program until the counter is 0.
public class Countdownlatchtest {public static void main (string[] args) {Executorservice service = Executors.newcachedthr Eadpool (); final Countdownlatch cdorder = new Countdownlatch (1); final Countdownlatch cdanswer = new Countdownlatch (3); for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try {System.out.println ("thread" + thread.currentthread (). GetName () + " Being prepared to accept the order "); cdorder.await (); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "accepted command"); Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "response command processing result"); Cdanswer.countdown (); catch (Exception e) {e.printstacktrace ();}}}; Service.execute (runnable);} try {thread.sleep ((long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Threads" + thread.currentthread (). GetName () + "upcoming Commands"); Cdorder.countdown (); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "command sent, waiting for results"); cdanswer.await (); SYSTEM.OUT.PRINTLN ("Threads" + thread.currentthread (). GetName () + "All response results received");} CatcH (Exception e) {e.printstacktrace ();} Service.shutdown ();}}
The above example initializes two countdownlatch examples, each passing in the code following the initial value 1 and 3,cdorder.await (), which will be blocked until Cdorder.countdown () is called and then executed downwards; cdanswer.await () The code behind is blocked, called three times Cdanswer.countdown () to execute down.
So the above results are executed:
Thread pool-1-thread-1 is preparing to accept commands
Thread pool-1-thread-3 is preparing to accept commands
Thread Pool-1-thread-2 is preparing to accept commands
Thread main is about to publish commands
Thread main has sent commands and is waiting for results
Thread Pool-1-thread-1 accepted command
Thread Pool-1-thread-2 accepted command
Thread Pool-1-thread-3 accepted command
Thread pool-1-thread-2 Response Command processing result
Thread pool-1-thread-3 Response Command processing result
Thread pool-1-thread-1 Response Command processing result
Thread main has received all response results
Await (long timeout, timeunit unit), is waiting for a certain time, then executes, regardless of whether the counter is to 0.
The following example sources: http://www.blogjava.net/freeman1984/archive/2011/07/04/353654.html
10 students on the bus, the car waiting for students to get on the bus, if there is a waiting time limit, to the time to leave, regardless of the students did not finish. If there is no waiting time, the student is finished before opening:
public class Countdownlatchtest {public static int numberofpeople = The number of students waiting for 10;//public static Boolean isgone = Fals e;//car Open sign public static int carwaittime = 3;//car etc time public static void main (string[] args) throws Interruptedexc eption {Countdownlatch Waitstudentsgeton = new Countdownlatch (numberofpeople); New Thread (New Geton (Waitstudentsgeton)). Start (); Waitstudentgeton (Waitstudentsgeton);//etc All students get on the bus drivehome ();//drive away} private static void WA Itstudentgeton (Countdownlatch Waitstudentsgeton) throws Interruptedexception {System.out.println ("Hurry up, hurry up and get on the bus."); Waitstudentsgeton.await (Carwaittime, timeunit.seconds);//wait 5 seconds, not to get on the car, drive away. } private static void Drivehome () throws Interruptedexception {System.out.println ("drive, the shoe breaks the cloak of the coat, you laugh at me, he laughs at me. The fan is broken "); 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 ("mother, still Poor:" +waitstudentsgeton.getcount () + "No baby on the bus . How did the car Go "); Break } Boolean goonsuccess = new Student (i+1). Geton ();//order boarding if (goonsuccess) Waitstudentsgeton.cou Ntdown (); } catch (Interruptedexception e) {} if (Waitstudentsgeton.getcount ()!=0l) {System.out.pri Ntln ("Still Poor:" + (Waitstudentsgeton.getcount ()) + "no Car"); }else{System.out.println ("All Aboard"); }}} class student{private int mynum;//student number public Student (int num) { This.mynum = num; }//Boarding public boolean Geton ()Throws interruptedexception{Thread.CurrentThread (). Sleep (New Random (). Nextint (2) *1000);//pick-up time to use, random if (countdownlatchtest.isgone) {return false;//cannot be on, the car fails} System.out.print ("Numbered : "+mynum+" of the students on the bus ...); return true; } }}
‘
Countdownlatch of the Java Synchronization tool class