Java Concurrency - 浅析 CountDownLatch 的用法

The Java concurrency API provides a class that allows one or more threads to wait until a set of operations are made. It's the CountDownLatch class. This class is initialized with an integer number, which is the number of operations the threads are going to wait for. When a thread wants to wait for the execution of these operations, it uses the await() method. This method puts the thread to sleep until the operations are completed. When one of these operations finishes, it uses the countDown() method to decrement the internal counter of the CountDownLatch class. When the counter arrives to 0, the class wakes up all the threads that were sleeping in the await() method.

Example

In this recipe, you will learn how to use the CountDownLatch class implementing a videoconference system. The video-conference system will wait for the arrival of all the participants before it begins.

Videoconference

/**
* This class implements the controller of the Videoconference
* It uses a CountDownLatch to control the arrival of all the participants in the conference.
*/
public class Videoconference implements Runnable { private final CountDownLatch controller; public Videoconference(int number) {
controller = new CountDownLatch(number);
} /**
* This method is called by every participant when he incorporates to the VideoConference
* @param participant
*/
public void arrive(String name) {
System.out.printf("%s has arrived.\n", name);
// This method uses the countDown method to decrement the internal counter of the CountDownLatch
controller.countDown();
System.out.printf("VideoConference: Waiting for %d participants.\n", controller.getCount());
} /**
* This is the main method of the Controller of the VideoConference.
* It waits for all the participants and then, starts the conference
*/
@Override
public void run() {
System.out.printf("VideoConference: Initialization: %d participants.\n", controller.getCount());
try {
// Wait for all the participants
controller.await();
// Starts the conference
System.out.printf("VideoConference: All the participants have come\n");
System.out.printf("VideoConference: Let's start...\n");
} catch (InterruptedException e) {
e.printStackTrace();
}
} } /**
* This class implements a participant in the VideoConference
*/
public class Participant implements Runnable { /**
* VideoConference in which this participant will take part off
*/
private Videoconference conference; /**
* Name of the participant. For log purposes only
*/
private String name; /**
* Constructor of the class. Initialize its attributes
*/
public Participant(Videoconference conference, String name) {
this.conference = conference;
this.name = name;
} /**
* Core method of the participant. Waits a random time and joins the VideoConference
*/
@Override
public void run() {
Long duration = (long) (Math.random() * 10);
try {
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
conference.arrive(name);
} } /**
* Main class of the example. Create, initialize and execute all the objects necessaries for the example
*/
public class Main {
public static void main(String[] args) {
// Creates a VideoConference with 10 participants.
Videoconference conference = new Videoconference(10);
// Creates a thread to run the VideoConference and start it.
Thread threadConference = new Thread(conference);
threadConference.start(); // Creates 10 participants, a thread for each one and starts them
for (int i = 0; i < 10; i++) {
Participant p = new Participant(conference, "Participant" + i);
Thread t = new Thread(p);
t.start();
} }
}

The CountDownLatch class has three basic elements:

  • The initialization value that determines how many events the CountDownLatch class waits for
  • The await() method, called by the threads that wait for the finalization of all the events
  • The countDown() method, called by the events when they finish their execution

When you create a CountDownLatch object, the object uses the constructor's parameter to initialize an internal counter. Every time a thread calls the countDown() method, the CountDownLatch object decrements the internal counter in one unit. When the internal counter arrives to 0, the CountDownLatch object wakes up all the threads that were waiting in the await() method.

There's no way to re-initialize the internal counter of the CountDownLatch object or to modify its value. Once the counter is initialized, the only method you can use to modify its value is the countDown() method explained earlier. When the counter arrives to 0, all the calls to the await() method return immediately and all subsequent calls to the countDown() method have no effect.

The CountDownLatch class has another version of the await() method, which is given as follows:

await(long time, TimeUnit unit): The thread will be sleeping until it's interrupted; the internal counter of CountDownLatch arrives to 0 or specified time passes. The TimeUnit class is an enumeration with the following constants: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, and SECONDS.

上一篇:Noah的学习笔记之Python篇:函数“可变长参数”


下一篇:Noah的学习笔记之Python篇:装饰器