CountDownLatch works in latch principle, main thread will wait until gate is open. One thread waits for n number of threads specified while creating CountDownLatch in Java.
Any thread, usually main thread of application, which calls CountDownLatch.await( ) will wait until count reaches zero or its interrupted by another thread. All other thread are required to do count down by calling CountDownLatch.countDown( ) once they are completed or ready.
As soon as count reaches zero, Thread awaiting starts running. One of the disadvantages/advantages of CountDownLatch is that its not reusable once count reaches to zero you can not use CountDownLatch any more.
And Dialer
Main
Any thread, usually main thread of application, which calls CountDownLatch.await( ) will wait until count reaches zero or its interrupted by another thread. All other thread are required to do count down by calling CountDownLatch.countDown( ) once they are completed or ready.
As soon as count reaches zero, Thread awaiting starts running. One of the disadvantages/advantages of CountDownLatch is that its not reusable once count reaches to zero you can not use CountDownLatch any more.
public class Audioconference implements Runnable{ // This class uses a CountDownLatch to control the arrivel of all the participants private final CountDownLatch controller; // Constructor of the class. Initializes the CountDownLatch @param number The number of participants in the Audioconference public Audioconference(int number) { controller=new CountDownLatch(number); } // This method is called by every participant when he incorporates to the Audioconference * @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("Audioconference: Waiting for %d participants.\n",controller.getCount()); } // This is the main method of the Controller of the Audioconference. It waits for all the participants and the, starts the conference @Override public void run() { System.out.printf("Audioconference: Initialization: %d participants.\n",controller.getCount()); try { // Wait for all the participants controller.await(); // Starts the conference System.out.printf("Audioconference: All the participants have come\n"); System.out.printf("Audioconference: Let's start...\n"); } catch (InterruptedException e) { e.printStackTrace(); } } }
And Dialer
public class Dialer implements Runnable { // Audioconference in which this Dialer will take part off private Audioconference conference; // Name of the Dialer. For log purposes only private String name; /** * Constructor of the class. Initialize its attributes * @param conference Audioconference in which is going to take part off * @param name Name of the Dialer */ public Dialer(Audioconference conference, String name) { this.conference=conference; this.name=name; } // Core method of the Dialer. Waits a random time and joins the Audioconference @Override public void run() { Long duration=(long)(Math.random()*10); try { TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } conference.arrive(name); } }
Main
public static void main(String[] args) { // Creates a Audioconference with 10 Dialers. Audioconference conference=new Audioconference(10); // Creates a thread to run the Audioconference and start it. Thread threadConference=new Thread(conference); threadConference.start(); // Creates ten Dialers, a thread for each one and starts them for (int i=0; i<10; i++){ Dialer p=new Dialer(conference, "Dialer "+i); Thread t=new Thread(p); t.start(); } }
No comments:
Post a Comment