Follow these steps to implement the example:
- Create a class named Videoconference and specify that it implements the Runnable interface. This class will implement the video conference system:
public class Videoconference implements Runnable{
- Declare a CountDownLatch object named controller:
private final CountDownLatch controller;
- Implement the constructor of the class that initializes the CountDownLatch attribute. The Videoconference class will wait for the arrival of the number of participants received as a parameter:
public Videoconference(int number) {
controller=new CountDownLatch(number);
}
- Implement the arrive() method. This method will be called each time a participant arrives for the video conference. It receives a String type named name as the parameter:
public void arrive(String name){
- First, it writes a message with the parameter it has received:
System.out.printf("%s has arrived.",name);
- Then, it calls the countDown() method of the CountDownLatch object:
controller.countDown();
- Finally, it writes another message with the number of participants whose arrival is pending, using the getCount() method of the CountDownLatch object:
System.out.printf("VideoConference: Waiting for %d
participants. ",controller.getCount());
- Next, implement the main method of the video conference system. It's the run() method that every Runnable object must have:
@Override
public void run() {
- First, use the getCount() method to write a message with the number of participants in the video conference:
System.out.printf("VideoConference: Initialization: %d
participants. ",controller.getCount());
- Then, use the await() method to wait for all the participants. As this method can throw an InterruptedException exception, you must include the code to process it:
try {
controller.await();
- Finally, write a message to indicate that all the participants have arrived:
System.out.printf("VideoConference: All the participants have
come ");
System.out.printf("VideoConference: Let's start... ");
} catch (InterruptedException e) {
e.printStackTrace();
}
- Next, create the Participant class and specify that it implements the Runnable interface. This class represents each participant in the video conference:
public class Participant implements Runnable {
- Declare a private Videoconference attribute named conference:
private Videoconference conference;
- Declare a private String attribute named name:
private String name;
- Implement the constructor of the class that initializes both the preceding attributes:
public Participant(Videoconference conference, String name) {
this.conference=conference;
this.name=name;
}
- Implement the run() method of the participants:
@Override
public void run() {
- First, put the thread to sleep for a random period of time:
long duration=(long)(Math.random()*10);
try {
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
- Then, use the arrive() method of the Videoconference object to indicate the arrival of this participant:
conference.arrive(name);
- Finally, implement the main class of the example by creating a class named Main and adding the main() method to it:
public class Main {
public static void main(String[] args) {
- Next, create a Videoconference object named conference that waits for 10 participants:
Videoconference conference=new Videoconference(10);
- Create Thread to run this Videoconference object and start it:
Thread threadConference=new Thread(conference);
threadConference.start();
- Create 10 Participant objects, a Thread object to run each of them, and start all the threads:
for (int i=0; i<10; i++){
Participant p=new Participant(conference, "Participant "+i);
Thread t=new Thread(p);
t.start();
}