It seems that the countdown counter, calling the countdown method of the Countdownlatch object, minus the counter by 1. When it reaches 0, all the waiters start executing.
java.util.concurrent.countdownlaatch
A synchronous auxiliary class, before completing a set of operations in other threads, it allows one or more threads to wait. Use a given count to initialize the countdownlatch. Because the countdown () method is called, the Await method has been blocked before the current counting reaches zero. After that, all the waiting threads will be released, and all subsequent calls of Await will return immediately. This phenomenon only appears once -counting cannot be reset. If you need to reset the count, consider using CyclicBarrier.
Countdownlatch is a general synchronization tool, which has many uses. Use counting 1 initialized countownlatch as a simple opening/lock depositor, or the entrance: Before opening the entrance by calling the Countdown () thread call, all threads calling Await have been waiting at the entrance. Use N initials of countdownlatch can wait a thread to wait before the N thread is completed, or keep it waiting before a certain operation.
One of the useful features of the countdownlatch is that it does not require the thread that calls the countdown method to continue until it is counted at zero, and before all threads can pass through, it just prevents any thread from continuing through an Await.
Example: Multiple athletes are waiting for the referee order: All athletes such as the referee and other athletes release the results after Qi Qi
package com.ljq.test.thread; Import Java.util.Concurrent.Countdownlaatch; Import Java.util.CURRENT.ExecutorService; nt.executors; Public Class CountdownlatchTest {Public Static Void Main (String [] Args) {ExecutorService Service = Executors.NewCacheDthreadpool (); // The counter of the referee issued a command, the counter is 0, and the athlete runs the final countdownlatch CDREDER = new Country (1). ; // The number of athletes ran to the finish line, and the referee announced the results of the result. Final Countdownlatch CDANSWER = New Countdownlatch (3); // Generate 3 athletes for (int i = 0; i <3; i ++) {Runnable Runnable = new runnable () {public void raun () {Try {System.out.println ("Thread" + thread.currentthread (). Getname () + "is preparing to accept commands"); cdorder.await (); System.out.println ("thread" + thread.currentthRead (). Has accepted commands "); thread.sleep ((long) (math.random ()*10000)); system.out.println (" thread " + thread.currentthread (). Getname () +" response command processing results " );;} Catch (Exception E) {e.printstacktrace ();}}; service.execute (runnable); // The athletes start the task} Thread .sleep ((Long) (math.random ()*10000)); System.out.println ("thread" + thread.currentthread (). Getname () + ""); cdorder.Countdown (); // The command counter is set to 0, and the command system.out.println ("thread" + thread.currentthread (). GetName () + "has sent command, waiting for the result"); cdansweer.await (); // wait for All athletes, the counter is 0 all the athletes in place System.out.println ("thread" + thread.currentthRead (). GetName () + "has received all the response results"); );} service.shutdown ();}}Back results:
The thread POOL-THREAD-3 is preparing to accept the command thread POOL-1-Thread-is preparing to accept the command thread POOL-Thread-2 is preparing to accept the command thread MAIN will soon send the command thread Main has sent the command, waiting for the result result, is waiting for the result result Thread Pool-Thread-2 has accepted the command thread POOL-1-Thread-1 POOL-1-Thread-3 has accepted the command thread POOL-Thread-3 to respond to the command processing result thread POOL-1- Thread-2 response to command processing results thread POOL-1-Thread-1 response command processing result thread main has received all the response results