document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package Demo8;
  2.  
  3. import java.util.concurrent.CountDownLatch;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6.  
  7. /**
  8.  * Title -- Advanced Java_ Multi-threading Part 6 -- Countdown Latches
  9.  *
  10.  * @author Dharmaraj.Net
  11.  */
  12. public class App {
  13.  
  14.     public static void main(String[] args) {
  15.         CountDownLatch latch = new CountDownLatch(3);
  16.         ExecutorService executor = Executors.newFixedThreadPool(3);
  17.         for (int i = 0; i < 3; i++) {
  18.             executor.submit(new Processor(latch));
  19.         }
  20.         try {
  21.             latch.await();
  22.         } catch (InterruptedException e) {
  23.             e.printStackTrace();
  24.         }
  25.         System.out.println("Completed");
  26.     }
  27.  
  28. }
  29.  
  30. class Processor implements Runnable {
  31.  
  32.     private CountDownLatch latch;
  33.  
  34.     public Processor(CountDownLatch latch) {
  35.         this.latch = latch;
  36.     }
  37.  
  38.     public void run() {
  39.         System.out.println("Started....");
  40.        
  41.         try {
  42.             Thread.sleep(3000);
  43.         } catch (InterruptedException e) {
  44.             e.printStackTrace();
  45.         }
  46.        
  47.         latch.countDown();
  48.     }
  49.  
  50. }
');