Countdownlatch
1, Class introduction
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. a thread (or more), waiting for another n a thread complete something
2. Usage Scenarios
in some applications, you need to wait for a certain condition to meet the requirements before you can do the following, and when the thread is finished, the event is triggered for later action. You can use Countdownlatch at this time. Countdownlatch the most important method is countdown () and await (), the former is mainly a countdown, the latter is waiting for the countdown to 0, if not reached 0, only blocking wait. 3. Method description await
await (Long timeout, Timeunit unit) throws Interruptedexception
-
Causes the current thread to wait until the latch is counted down to 0, unless the thread is interrupted or the specified wait time is exceeded. If the current count is zero, this method returns immediately
true
Value.
If the current count is greater than 0, the current thread is disabled for thread scheduling purposes, and the thread will remain dormant until one of the following three situations occurs:
countDown()
method, the count reaches 0;
- One of the other threads interrupts the current thread, or
- The specified wait time has been exceeded.
If the count reaches 0, the method returns a true
value.
- The interrupt state of the thread has been set when entering this method;
- is interrupted while waiting,
InterruptedException
, and clears the interrupted state of the current thread. If the specified wait time is exceeded, the return value is false
. If the time is less than or equal to zero, this method does not wait at all.
-
-
-
Parameters:
-
-The maximum
-
timeout
amount of time to wait
-
-
unit
-
timeout
The time unit of the parameter.
-
-
Return:
-
Returns
-
if the count reaches 0, or
true
if the wait time
false
is exceeded before the count reaches 0
-
-
Thrown:
-
-
InterruptedException
-If the current thread is interrupted while waiting
-
ImportJava.util.concurrent.CountDownLatch;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classCountdownlatchtest {//The 100-metre race was simulated and 10 players were ready to wait until the referee commanded. When everyone gets to the finish line, the game is over. Public Static voidMain (string[] args)throwsinterruptedexception {//start of the countdown lock FinalCountdownlatch begin =NewCountdownlatch (1); //end of the countdown lock FinalCountdownlatch end =NewCountdownlatch (10); //10 Contestants FinalExecutorservice exec = Executors.newfixedthreadpool (10); for(intindex = 0; Index < 10; index++) { Final intNO = index + 1; Runnable Run=NewRunnable () { Public voidrun () {Try { //if the current count is zero, this method returns immediately. //waitbegin.await (); Thread.Sleep ((Long) (Math.random () * 10000)); System.out.println ("No." + No + "arrived"); } Catch(Interruptedexception e) {}finally { //when each player reaches the end, end is reduced by oneEnd.countdown (); } } }; Exec.submit (run); } System.out.println ("Game Start"); //Begin minus one and start playingBegin.countdown (); //wait for end to become 0, that is, all players reach the endend.await (); System.out.println ("Game over"); Exec.shutdown (); }}
Output Result:
Game startno.7 arrivedno.5 arrivedno.9 arrivedno.4 arrivedno.8 arrivedno.2 arrivedno.3 arrivedNo.6 arrivedNo.1 arrivedNo . Ten arrivedgame over
Reprint Address: http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html
Introduction to Java--countdownlatch