Java Concurrency of CountDownLatch (waiting for multiple concurrent events to complete) Introducing the CountDownLatch class Concrete examples of the CountDownLatch class CountDownLatch Summary


  • Introducing the CountDownLatch class
  • A concrete example of the CountDownLatch class
  • CountDownLatch Summary

Introducing the CountDownLatch class

Java introduced the CountDownLatch class after JDK 1.5. This class is a synchronization helper class. Used for a thread to wait for multiple operations to complete before executing, i.e. this current thread will keep blocking until the multiple operations it is waiting for have completed. First the CountDownLatch class is initialized, setting the number of operations it needs to wait for to complete. Then whenever an operation is completed, the countDown method is called, which decrements the counter inside CountDownLatch by one. When reduced to 0, the CountDownLatch class wakes up all threads that have gone dormant by calling the await method.

A concrete example of the CountDownLatch class

Without further ado, let's look at one specific example to understand the use of the CountDownLatch class.

Let's take the most intuitive example, let's say we need to have a video conference, and this conference needs to wait for a certain number of people to arrive before the conference starts. This situation would be ideal for using the CountDownLatch class for synchronization, that is, waiting for multiple concurrent events to occur, since the arrival of each participant is concurrent.

First we implement the meeting class, which holds an object of the CountDownLatch class and defines an arrive method that will be called whenever a meeting person arrives, telling the meeting that it has arrived, and this method, which calls the CountDown method, will subtract the counter by one.

In the run method of the meeting class, before announcing the start of the meeting, the await method of the CountDownLatch class is called to sleep until countDown decreases to 0, that is, the counter decreases to 0, indicating that all people have arrived, before waking up the code that continues the thread and announcing the start of the meeting

package CountDown;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class VideoConference implements Runnable  {
    
    private final CountDownLatch controller;
    
    public VideoConference(int number) {
        controller = new CountDownLatch(10);
    }
    
    public synchronized void arrive(String name) {
        
        
        System.out.println(name + " has arrived.");
        
        System.out.println("VideoConference : waiting for " + (controller.getCount() - 1) + " partivipants");
        
        
        controller.countDown();
    }
    
    @Override
    public void run() {
        System.out.println("VideoConference : Innitiaization : " + controller.getCount() + " participants");
        
        try {
            controller.await();
            System.out.println("All people has come");
            System.out.println("Let's start...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Next we implement the participant's class, which also implements the runnable interface, and first it holds a meeting object in order to call the drive method on execution to tell the meeting that the participant has arrived.

package CountDown;

import java.util.concurrent.TimeUnit;

public class Participant implements Runnable {

    private VideoConference conference;
    
    private String name;
    
    public Participant(VideoConference conference, String name) {
        this.conference = conference;
        this.name = name;
    }
    
    
    @Override
    public void run() {
        long duration = (long)Math.random() * 10;
        
        try {
            TimeUnit.SECONDS.sleep(duration + 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        conference.arrive(name);
    }

}

Finally we implement the test class

package CountDown;

public class Main {

    public static void main(String[] args) {
        
        VideoConference conference = new VideoConference(10);
        
        new Thread(conference).start();
        
        for(int i=0;i<10;i++) {
            Participant p = new Participant(conference, "Participant " + i);
            new Thread(p).start();
        }
    }
}

Results of the run.

image.png

The await method also allows you to specify the time of hibernation, when the hibernation time is up or the counter is reduced to 0, it will wake up all the threads that are hibernated by CountDownLatch, then we can use this hibernation time to set here, we will only wait for the middle of 10s, if in 10s, there are still people who haven't arrived, we don't care and start the session first.

Only a simple modification to the run method is required.

@Override
    public void run() {
        System.out.println("VideoConference : Innitiaization : " + controller.getCount() + " participants");
        
        try {
            controller.await(10, TimeUnit.SECONDS);
            if(controller.getCount() == 0) {
                System.out.println("All people has come");
                System.out.println("Let's start...");
            }
            else {
                System.out.println(" No more waiting.");
                System.out.println("Let's start...");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

running result

image.png

CountDownLatch Summary

CountDownLatch has three basic elements.

  • An initial value that defines the number of concurrent threads that must wait for completion
  • The await method, called by the thread that needs to wait until the other operation completes first, will first hibernate the thread until the other operation completes and the counter decreases to 0 before waking up the therefore hibernating thread
  • The countDown method, called after the completion of each event being waited for, will subtract the counter by one
  • CountDownLatch is not used to protect critical areas and shared resources, it is used to synchronize the execution of threads and operations
  • CountDownLatch is disposable, when the counter is reduced to 0, this class is useless, and any subsequent operations on it will not work, so we need to create a new countDownLatch class

Recommended>>
1、How community products create value with users personal sediment content Community App Discussion ②
2、Elasticwwwwjs666netComputeI309439III2ServiceECS
3、Hands on with the Top 20 Superstore Sales Forecast
4、SpringCloud Overview
5、Glusterfs rpc module source code analysis above of RPC overview

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号