The literal translation is the inverted count (Countdown) latch (Latch). Count Needless to say, the meaning of the latch is to prevent progress. This means that the countdownlatch.await () method blocks the current thread before it counts backwards to 0.
Countdownlatch is a synchronization helper class that allows one or more threads to wait until a set of operations is performed on another thread.
The role of Countdownlatch is similar to the Thread.Join () method, which can be used for collaboration between a set of threads and another set of threads. For example, the main thread needs a series of preparations before doing a job, and only if the preparation is complete will the main thread continue its work. These preparations are independent of each other, so they can be executed concurrently to improve speed. In this scenario, you can use countdownlatch to coordinate the scheduling between threads. In the era of direct creation of threads (before Java 5.0), we can use Thread.Join (). After JUC appears, because threads in the thread pool cannot be referenced directly, you must use Countdownlatch.
The Countdownlatch class is a synchronous counter that passes in the int parameter, which is the initial value of the counter, Countdown () method, each time the counter is reduced by 1, the counter is greater than 0 o'clock, and the await () method blocks the program from continuing execution. Countdownlatch can be seen as an inverted-count latch that triggers a specific event when the count is reduced to 0 o'clock. With this feature, you can let the main thread wait for the end of the child thread. The following is illustrated by an example of a mock athlete contest.
A very typical scenario for Countdownlatch is that you have a task that you want to perform down, but you have to wait until the other task has finished executing before you can proceed down. If the task we want to continue to execute calls a Countdownlatch object's await () method, and the other task finishes its own task, invokes the countdown () method on the same Countdownlatch object, which await () The task of the method blocks the wait until the count of the Countdownlatch object is reduced to 0.
Countdownlatch Function List
Countdownlatch (int count)
constructs a countdownlatch initialized with a given count.
//Make the current thread wait until the latch is counted down to 0 unless the thread is interrupted.
void await ()
//enables the current thread to wait until the latch is counted up to 0 unless the thread is interrupted or the specified wait time is exceeded.
Boolean await (long timeout, timeunit unit)
//Descending latch count, if the count reaches 0, all waiting threads are freed.
void Countdown ()
//returns the current count.
long GetCount ()
//Returns a string identifying this latch and its status.
String toString ()
Countdownlatch Data structure
the UML class diagram for Countdownlatch is as follows:
Countdownlatch's data structure is simple and is implemented through "shared locks." It contains the Sync object, and sync is the sync type. Sync is an instance class that inherits from Aqs.
examples of the use of Countdownlatch
the following is implemented by Countdownlatch: The main thread waits until all 5 child threads complete the specified work (Hibernate 1000ms) before continuing.
Import Java.util.concurrent.CountDownLatch;
Import Java.util.concurrent.CyclicBarrier;
public class CountDownLatchTest1 {
private static int latch_size = 5;
private static Countdownlatch donesignal;
public static void Main (string[] args) {
try {
donesignal = new Countdownlatch (latch_size);
Create a new 5 task for
(int i=0; i<latch_size; i++)
new Innerthread (). Start ();
System.out.println ("main await begin.");
The main thread waits for the completion of 5 tasks in the thread pool
donesignal.await ();
System.out.println ("main await finished.");
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
Static class Innerthread extends thread{public
void Run () {
try {
thread.sleep (1000);
System.out.println (Thread.CurrentThread (). GetName () + "sleep 1000ms.");
Subtract the value of the Countdownlatch by 1
donesignal.countdown ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
}
}
Run Result:
Main await begin.
Thread-0 sleep 1000ms.
Thread-2 sleep 1000ms.
Thread-1 sleep 1000ms.
Thread-4 sleep 1000ms.
Thread-3 sleep 1000ms.
Main await finished.
The results show that the main thread donesignal.await () waits for other threads to decrement the donesignal to 0. Each of the other 5 innerthread threads, each of which is reduced by 1 by Donesignal.countdown (), and when the donesignal is 0 o'clock, main is awakened and continues execution.
The difference between Ps:countdownlatch and Cyclicbarrier:
(1) The role of Countdownlatch is to allow 1 or n threads to wait for other threads to complete execution, while Cyclicbarrier allows n threads to wait on each other.
(2) Countdownlatch counters cannot be reset, cyclicbarrier counters can be reset and used, so it is called the barrier of the loop.