Like a countdown counter, the countdown method that calls the Countdownlatch object will subtract 1 from the counter, and when it arrives 0 o'clock, all the waiting will start executing.
Java.util.concurrent.CountDownLatch
A synchronous helper class that allows one or more threads to wait until a set of operations that are performed in another thread is completed. Initializes the countdownlatch with the given count. Because the countdown () method is called, the await method is blocked until the current count reaches 0. After that, all the waiting threads are freed, and all subsequent calls to await are returned immediately. This behavior occurs only once-the count cannot be reset. If you need to reset the count, consider using Cyclicbarrier.
Countdownlatch is a universal synchronization tool that has many uses. Countdownlatch, which counts 1 initialized, is used as a simple on/Guan, or Portal: All threads that call await are waiting at the entrance until a thread that calls Countdown () opens the portal. N-initialized Countdownlatch can cause a thread to wait until N threads complete an operation, or to wait until an operation completes N.
A useful feature of Countdownlatch is that it does not require a thread that calls the countdown method to wait until the count arrives 0 o'clock, and before all threads can pass, it simply prevents any thread from continuing through an await.
Example: Multiple athletes waiting for the referee to order: The referee and all the athletes to post the results
Package Com.ljq.test.thread;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class CountdownLatchTest {public static void Main (string[] args) {Executorservice service = Executors.newcachedthreadpool ();//The referee publishes the counter of the command, the counter is 0, The athlete runs final countdownlatch cdorder = new Countdownlatch (1);//The player runs to the end of the counter, announcing the result for 0 Referee final Countdownlatch cdanswer = new Countdownlatch (3);//Generate 3 players for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try { SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "ready to accept Command"); 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); The athlete starts the task}try {//The referee will take a break and then release the Command Thread.Sleep ((long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.)CurrentThread (). GetName () + "forthcoming command"); Cdorder.countdown (); The command counter is set to 0, and the command System.out.println ("thread" + thread.currentthread (). GetName () + "sent command, waiting for results"); cdanswer.await (); Wait for all players, counter for 0 all athletes in place System.out.println ("thread" + thread.currentthread (). GetName () + "All responses received");} catch (Exception e) {e.printstacktrace ();} Service.shutdown ();}}
return Result:
Thread pool-1-thread-3 is preparing to accept the command thread pool-1-thread-1 is preparing to accept the command thread pool-1-thread-2 is preparing to accept the command thread, main is about to publish the command thread, main has sent the command, Waiting for result thread pool-1-thread-2 accepted command thread pool-1-thread-1 accepted command thread pool-1-thread-3 accepted command thread pool-1-thread-3 response command processing result thread pool-1-thread-2 response command processing knot Fruit Thread pool-1-thread-1 Response command processing result thread main has received all response results
Java5 countdownlatch Sync Tool