Saturday, 11 January 2014

Advanced Java_ Multi-threading Part 6 -- Countdown Latches


==
package Demo8;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Title -- Advanced Java_ Multi-threading Part 6 -- Countdown Latches
 * 
 * @author Dharmaraj.Net
 */
public class App {

 public static void main(String[] args) {
  CountDownLatch latch = new CountDownLatch(3);
  ExecutorService executor = Executors.newFixedThreadPool(3);
  for (int i = 0; i < 3; i++) {
   executor.submit(new Processor(latch));
  }
  try {
   latch.await();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println("Completed");
 }

}

class Processor implements Runnable {

 private CountDownLatch latch;

 public Processor(CountDownLatch latch) {
  this.latch = latch;
 }

 public void run() {
  System.out.println("Started....");
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  latch.countDown();
 }

}

No comments:

Post a Comment