CountDownLatch

concept

Let some threads block until another thread has completed a series of operations before being awakened

CountDownLatch has two main methods, and when one or more threads call the await method, the calling thread is blocked. Other threads calling CountDown will subtract 1 from the counter (the thread calling CountDown will not be blocked), and when the counter value becomes zero, the thread blocked by calling await method will be woken up and continue execution

scenario

Now there is such a scenario, suppose that a study lounge has 7 people, is one of the monitor, the monitor’s main duty is in the other six students go after turn off the lights and lock the door, and then leave, so the monitor is need to be the last to leave, so what are the methods to control monitor this thread is the last one, and other random execution is thread

The solution

This is where the CountDownLatch, the counter, comes in. We create a total of 6 threads and set the counter to 6

 / / counter
 CountDownLatch countDownLatch = new CountDownLatch(6);
Copy the code

Then each time the student thread finishes executing, the counter is subtracted by one

 for (int i = 0; i <= 6; i++) {
     new Thread(() -> {
         System.out.println(Thread.currentThread().getName() + "\ T left the classroom after self-study");
         countDownLatch.countDown();
     }, String.valueOf(i)).start();
 }
Copy the code

Finally, we need to control the execution of the main thread of the monitor through the await method of CountDownLatch. Countdownlatch.await () can be thought of as a wall, and only when the counter value is zero will the wall go away and the main thread continue executing

 countDownLatch.await();
 ​
 System.out.println(Thread.currentThread().getName() + "\t monitor closes the door last");
Copy the code

Without CountDownLatch, we see that the main thread has completed ahead of time

 1Finish your study and leave the classroom0After studying, leave the classroom The monitor finally closes the door2Finish your study and leave the classroom3Finish your study and leave the classroom4Finish your study and leave the classroom5Finish your study and leave the classroom6Finish your study and leave the classroomCopy the code

With the result of CountDownLatch, we can control the execution of the main method, thus ensuring the execution of the previous tasks

 0Finish your study and leave the classroom2Finish your study and leave the classroom4Finish your study and leave the classroom1Finish your study and leave the classroom5Finish your study and leave the classroom6Finish your study and leave the classroom3After studying, leave the classroom The monitor finally closes the doorCopy the code

The complete code

 package com.company;
 ​
 import java.util.concurrent.CountDownLatch;
 ​
 / * * *@author shoukailiang
  * @version 1.0
  * @date2021/8/7 returned from * /
 class CountDownLatchDemo {
     public static void main(String[] args) throws InterruptedException {
         CountDownLatch countDownLatch = new CountDownLatch(6);
         for (int i = 0; i < 6; i++) {
             new Thread(()->{
                 System.out.println(Thread.currentThread().getName() + "\ T left the classroom after self-study");
                 countDownLatch.countDown();
             },String.valueOf(i)).start();
         }
         countDownLatch.await();
         System.out.println(Thread.currentThread().getName() + "\t monitor closes the door last"); }}Copy the code