Java ThreadPoolExecutor extends

Description

Java ThreadPoolExecutor extends

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
class MyExecutor extends ThreadPoolExecutor {
  private ConcurrentHashMap<String, Date> startTimes;

  public MyExecutor(int corePoolSize, int maximumPoolSize,
      long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    startTimes=new ConcurrentHashMap<>();
  }//from www . ja  v a 2s.  c  o m

  @Override
  public void shutdown() {
    System.out.printf("shutdown.\n");
    System.out.printf("Executed: %d\n",getCompletedTaskCount());
    System.out.printf("Running : %d\n",getActiveCount());
    System.out.printf("Pending : %d\n",getQueue().size());
    super.shutdown();
  }

  @Override
  public List<Runnable> shutdownNow() {
    System.out.printf("immediately shutdown.\n");
    System.out.printf("Executed: %d\n",getCompletedTaskCount());
    System.out.printf("Running : %d\n",getActiveCount());
    System.out.printf("Pending : %d\n",getQueue().size());
    return super.shutdownNow();
  }

  @Override
  protected void beforeExecute(Thread t, Runnable r) {
    System.out.printf("A task is beginning: %s : %s\n",t.getName(),r.hashCode());
    startTimes.put(String.valueOf(r.hashCode()), new Date());
  }

  @Override
  protected void afterExecute(Runnable r, Throwable t) {
    Future<?> result=(Future<?>)r;
    try {
      System.out.printf("A task is finishing.\n");
      System.out.printf("Result: %s\n",result.get());
      Date startDate=startTimes.remove(String.valueOf(r.hashCode()));
      Date finishDate=new Date();
      long diff=finishDate.getTime()-startDate.getTime();
      System.out.printf("Duration: %d\n",diff);
    } catch (InterruptedException  | ExecutionException e) {
      e.printStackTrace();
    }
  }
}
 class SleepTwoSecondsTask implements Callable<String> {
  public String call() throws Exception {
    TimeUnit.SECONDS.sleep(2);
    return new Date().toString();
  }
}

public class Main {
  public static void main(String[] args) {

    MyExecutor myExecutor=new MyExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>());
    
    List<Future<String>> results=new ArrayList<>();
    
    for (int i=0; i<10; i++) {
      SleepTwoSecondsTask task=new SleepTwoSecondsTask();
      Future<String> result=myExecutor.submit(task);
      results.add(result);
    }
    for (int i=0; i<5; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    myExecutor.shutdown();
    
    for (int i=5; i<10; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    try {
      myExecutor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    
    System.out.printf("Main: End of the program.\n");
  }

}



PreviousNext

Related