Facebook
From shafin, 2 Weeks ago, written in Java.
Embed
Download Paste or View Raw
Hits: 131
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class Main extends Thread{
  7.     static final int MAX = 5;
  8.     private String name;
  9.  
  10.     public Main(String name) {
  11.         this.name = name;
  12.     }
  13.  
  14.     @Override
  15.     public void run() {
  16.         try {
  17.             for(int i=0; i<MAX; i++){
  18.                 if(i==0){
  19.                     Date d = new Date();
  20.                     SimpleDateFormat dt = new SimpleDateFormat("hh:mm:ss");
  21.                     System.out.println("Initialization time at task name: " +name + " = " + dt.format(d));
  22.                 }else{
  23.                     Date d = new Date();
  24.                     SimpleDateFormat dt = new SimpleDateFormat("hh:mm:ss");
  25.                     System.out.println("Execution time task name: " + name + " = " + dt.format(d));
  26.                 }
  27.                 Thread.sleep(10000);
  28.             }
  29.  
  30.         }catch (InterruptedException e){
  31.             System.out.println(e);
  32.         }
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         ExecutorService pool = Executors.newFixedThreadPool(MAX);
  37.         for (int i=0; i<10; i++){
  38.            Runnable r = new Main("TASK "+(i+1));
  39.            pool.execute(r);
  40.        }
  41.         pool.shutdown();
  42.     }
  43. }