CountDownLatch instructions are for your reference. The specific content is as follows
CountDownLatch is a java.util.concurrent package next synchronization tool class that allows one or more threads to wait until a set of operations is completed in other threads.
The usage of CountDownLatch is very simple. The following example is also what I saw online, and it is very appropriate. I will post it here.
public class Test { public static void main(String[] args) { CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(2); for(int i=0; i<2; i++){ Thread thread = new Thread(new Player(begin,end)); thread.start(); } try{ System.out.println("the race begin"); begin.countDown(); end.await(); System.out.println("the race end"); }catch(Exception e){ e.printStackTrace(); } }}/** * Player*/class Player implements Runnable{ private CountDownLatch begin; private CountDownLatch end; Player(CountDownLatch begin,CountDownLatch end){ this.begin = begin; this.end = end; } public void run() { try { begin.await(); System.out.println(Thread.currentThread().getName() + " arrived !");; end.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } }}Below are the running results
You can see that through the use of CountDownLatch, we control the execution order of threads.
In the above code, we use the await() method and the countDown() method. Let's verify their respective functions.
First verify the await() method. Comment out the end.await() in the main method. The following is the run result after commenting out.
You can see that the main thread did not wait for the thread representing the contestant to end, and directly announced that the competition was over! The game that ended at the beginning---
It can be seen here that the await() method has a blocking effect
Secondly, let's verify the countDown method, and comment on the end.countDown() in the contestant's thread. The following is the running result.
The program has been running, and all the players have reached the finish line, but the referee just doesn't promote the end of the game. What are he waiting for?
We guess that the countDown() method has the effect of waking up the blocking thread.
Then we may ask, since it has the function of waking up the blocking thread, isn't it possible to wake up the blocked main thread if we only call the countDown() method once?
Let's try it, uncomment the above coutDown() annotation, create a player again, the code is as follows
class Player2 implements Runnable{ private CountDownLatch begin; private CountDownLatch end; Player2(CountDownLatch begin,CountDownLatch end){ this.begin = begin; this.end = end; } public void run() { try { begin.await(); System.out.println(Thread.currentThread().getName() + " arrived !");// end.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }The main method is also modified as follows, creating two different players
public static void main(String[] args) { CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(2); Thread thread = new Thread(new Player(begin, end)); thread.start(); Thread thread2 = new Thread(new Player2(begin, end)); thread2.start(); try { System.out.println("the race begin"); begin.countDown(); end.await(); System.out.println("the race end"); } catch (Exception e) { e.printStackTrace(); } }Run it, here are the results
The main program has been blocked and has not been awakened. The referee has been using the toilet for a while!
It seems that countDown() does not directly wake up the thread, it is a bit like a counter, countdown.
Check the API documentation, sure enough, we added parameter 2 to the constructor, and we need to call countDown() 2 times to wake up the blocked thread of end.await().
CountDownLatch end = new CountDownLatch(2);
To sum up,
1. CountDownLatch end = new CountDownLatch(N); // When constructing an object, you need to pass in parameter N.
2. end.await() can block threads until the end.countDown() method is called N times before the thread is released.
3. end.countDown() can be called in multiple threads to calculate the number of calls that are the sum of all threads.
In the next blog, I will explain how CountDownLatch works from the source code level.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.