3.3 等待多个并发事件
Java并发API提供了一个允许一个或者多个线程等待直到一系列操作完成的类—CountDownLatch。这个类是一个integer数来初始化这个类的实例,这个数是线程将要等待执行操作数。当一个线程想要去等待这些操作的执行时,它使用await()方法。这个方法使线程睡眠直到所有的操作执行完成。当这些操作中的一个完成后,它使用countDown()方法减少CountDownLatch类的内部计数器的值。当这个计数器到达为0时,这个类将唤醒所有在await()方法中睡眠的线程
CountDownLath类有几个基本元素:
u 初始化的值将决定了ContDownLatch类中有多少个要等待的事件
u 当操作事件完成它们要执行的操作时,countDown()方法将被调用。
下面将介绍一个实例,阐述CountDownLatch的使用场合。假设要开会了, 只有所有的参会者到齐了,会起就开始。
定义会议Meeting类:
importjava.util.concurrent.CountDownLatch;
public class Meeting implements Runnable {
private finalCountDownLatch latch;
public Meeting(intnumber){
latch = new CountDownLatch(number);
}
public voidarrive(String name){
System.out.println(" "+ name + " arrived at the meeting."+ "Still waiting for "+
latch.getCount()+ " participant(s)");
latch.countDown();
}
@Override
public voidrun() {
System.out.println("Initializing the meeting and waiting for the "+ latch.getCount()+" participants");
try {
// Wait for all the participants
latch.await();
System.out.printf("All participants arrived,"+ ". Now the metting starts...\n");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public staticvoidmain(String[] args) {
//10 participants for the meeting
Meeting conference = new Meeting(10);
Thread tc = new Thread(conference, "meeting");
tc.start();
for(int i = 0; i < 10; i++){
Participant participant = new Participant(conference,"participant_"+ i);
Thread tp = new Thread(participant);
tp.start();
}
}
}
定义参会者类Participant:
import java.util.concurrent.TimeUnit;
public class Participant implements Runnable {
private Meeting conference;
private String name;
public Participant(Meeting conference, String name){
this.conference = conference;
this.name = name;
}
/**
* Defines the execution of some thread.
*/
@Override
public voidrun() {
long duration = (long)(Math.random()*100);
try {
System.out.println("thread name " + this.name+ " are waiting with "+duration + "s");
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
conference.arrive(name);
}
}
运行结果:
Initializingthe meeting and waiting for the 10 participants
threadname participant_0 are waiting with 4s
threadname participant_3 are waiting with 5s
threadname participant_9 are waiting with 8s
threadname participant_5 are waiting with 7s
threadname participant_8 are waiting with 4s
threadname participant_7 are waiting with 0s
threadname participant_6 are waiting with 4s
threadname participant_2 are waiting with 9s
threadname participant_4 are waiting with 4s
threadname participant_1 are waiting with 1s
participant_7 arrived at the meeting.Stillwaiting for 10 participant(s)
participant_1 arrived at the meeting.Stillwaiting for 9 participant(s)
participant_6 arrived at the meeting.Stillwaiting for 8 participant(s)
participant_8 arrived at the meeting.Stillwaiting for 8 participant(s)
participant_4 arrived at the meeting.Stillwaiting for 8 participant(s)
participant_0 arrived at the meeting.Stillwaiting for 8 participant(s)
participant_3 arrived at the meeting.Stillwaiting for 4 participant(s)
participant_5 arrived at the meeting.Stillwaiting for 3 participant(s)
participant_9 arrived at the meeting.Stillwaiting for 2 participant(s)
participant_2 arrived at the meeting.Stillwaiting for 1 participant(s)
All participants arrived,. Now the mettingstarts...
当所有的入会者到齐了,会议就开始了。某些时候,我们确实需要实现这样的功能。