How to do it...

Follow these steps to implement the example:

  1. 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{
  1. Declare a CountDownLatch object named controller:
        private final CountDownLatch controller;
  1. 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);
}
  1. 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){
  1. First, it writes a message with the parameter it has received:
        System.out.printf("%s has arrived.",name);
  1. Then, it calls the countDown() method of the CountDownLatch object:
        controller.countDown();
  1. 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());
  1. 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() {
  1. 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());
  1. 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();
  1. 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();
}
  1. 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 {
  1. Declare a private Videoconference attribute named conference:
        private Videoconference conference;
  1. Declare a private String attribute named name:
        private String name;
  1. Implement the constructor of the class that initializes both the preceding attributes:
        public Participant(Videoconference conference, String name) { 
this.conference=conference;
this.name=name;
}
  1. Implement the run() method of the participants:
        @Override 
public void run() {
  1. 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();
}
  1. Then, use the arrive() method of the Videoconference object to indicate the arrival of this participant:
        conference.arrive(name);
  1. 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) {
  1. Next, create a Videoconference object named conference that waits for 10 participants:
        Videoconference conference=new Videoconference(10);
  1. Create Thread to run this Videoconference object and start it:
        Thread threadConference=new Thread(conference); 
threadConference.start();
  1. 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();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.144.59