Advertisement
Guest User

Untitled

a guest
May 8th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class ThreadPoolFiddle {
  2.  
  3.     public static void main (String[] args) throws InterruptedException {
  4.         ExecutorService executorService = Executors.newFixedThreadPool(2);
  5.        
  6.         for(int i=0; i<10; i++) {
  7.             executorService.submit(new MyRunnable(i));
  8.         }
  9.        
  10.         executorService.shutdown();
  11.         executorService.awaitTermination(1, TimeUnit.DAYS);
  12.     }
  13. }
  14.  
  15. class MyRunnable implements Runnable {
  16.  
  17.     private int id;
  18.    
  19.     public MyRunnable(int id) {
  20.         this.id = id;
  21.     }
  22.    
  23.     @Override
  24.     public void run() {
  25.         System.out.println("Running " + id);
  26.         try {
  27.             Thread.sleep(5000);
  28.         } catch (InterruptedException e) {
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         }
  32.         System.out.println(id + " finished");
  33.     }
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement