document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package Demo7;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. /**
  8.  * Title -- Advanced Java_ Multi-threading Part 5 -- Thread Pools Note:--
  9.  *
  10.  * @author Dharmaraj.Net
  11.  */
  12. public class App {
  13.     public static void main(String[] args) {
  14.         ExecutorService executor = Executors.newFixedThreadPool(2); // Two
  15.                                                                     // factor
  16.                                                                     // workers
  17.                                                                     // two
  18.                                                                     // threads
  19.         for (int i = 0; i < 5; i++) {
  20.             executor.submit(new processor(i));
  21.         }
  22.         executor.shutdown();
  23.         System.out.println("All task submited");
  24.         try {
  25.             executor.awaitTermination(1, TimeUnit.DAYS);
  26.         } catch (InterruptedException e) {
  27.             e.printStackTrace();
  28.         }
  29.        
  30.         System.out.println("All task completed");
  31.     }
  32. }
  33.  
  34. class processor implements Runnable {
  35.  
  36.     private int id;
  37.  
  38.     public processor(int id) {
  39.         this.id = id;
  40.     }
  41.  
  42.     public void run() {
  43.         System.out.println("Starting :" + id);
  44.         try {
  45.             Thread.sleep(5000);
  46.         } catch (InterruptedException e) {
  47.             e.printStackTrace();
  48.         }
  49.         System.out.println("Complet :" + id);
  50.     }
  51.  
  52. }
');