Explain in detail the method of countdownlatch blocking threads in Java multithreaded programming
Count down latch. Needless to say, the meaning of the latch, as the name suggests, is to stop progress. This means countdownlatch The await () method blocks the current thread before the countdown is 0. Countdownlatch is a synchronization helper class that allows one or more threads to wait until a set of operations are completed in other threads. The role of countdownlatch and thread Similar to the join () method, it can be used for collaboration between one set of threads and another set of threads. For example, the main thread needs a series of preparations before doing a job. Only when these preparations are completed can 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 days when threads were created directly (before Java 5.0), we could use thread. Join(). After the occurrence of JUC, because the threads in the thread pool cannot be directly referenced, countdownlatch must be used. Countdownlatch class is a synchronous counter. During construction, an int parameter is passed in, which is the initial value of the counter. Every time the countdown() method is called, the counter decreases by 1. When the counter is greater than 0, the await() method will block the program from continuing to execute. Countdownlatch can be regarded as a countdown latch, which triggers a specific event when the count decreases to 0. With this feature, the main thread can wait for the end of the child thread. The following is an example of simulated athlete competition. A very typical application scenario of countdownlatch is that a task wants to be executed, but it can only be executed after other tasks are executed. If we want to continue to perform tasks that call a await () method of a CountDownLatch object, after the other tasks perform their own tasks, the countDown () method on the same CountDownLatch object is called, and the task of calling the await () method will remain blocked until the count value of the CountDownLatch object is reduced to 0.
Countdownlatch function list
The UML class diagram of countdownlatch data structure is as follows:
The data structure of countdownlatch is very simple. It is implemented through "shared lock". It contains sync objects, which are of sync type. Sync is an instance class that inherits from AQS.
The usage example of countdownlatch is implemented through countdownlatch below: the main thread waits until all the "5 sub threads" complete the "specified work (sleep 1000ms)" before continuing to run.
Operation results:
Result description: the main thread passes donesignal Await () waits for other threads to decrement donesignal to 0. The other five innerthread threads, each through donesignal Countdown() decrements the value of donesignal by 1; When donesignal is 0, main continues to execute after being awakened.
PS: the difference between countdownlatch and cyclicbarrier: (1) the function of countdownlatch is to allow 1 or n threads to wait for other threads to complete execution; Cyclicbarrier allows n threads to wait for each other. (2) The counter of countdownlatch cannot be reset; The cyclicbarrier counter can be used after being reset, so it is called a circular barrier.