Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPoolExecutorafterExecuteExample1 { public static void main(String[] args) { //Creating a thread pool ThreadPoolExecutor obj = new ThreadPoolExecutorafterExecuteExample1().new MyThreadExecutor(2, 3, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); //execute a runnable obj.execute(() -> { System.out.println("In Runnable!"); }); obj.shutdown(); } class MyThreadExecutor extends ThreadPoolExecutor { public MyThreadExecutor(int PoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue
workQueue) { super(PoolSize, maxPoolSize, keepAliveTime, unit, workQueue); } @Override protected void afterExecute(Runnable run, Throwable throw1) { super.afterExecute(run, throw1); if (throw1 == null) System.out.println("Completed!! without any Exception"); else System.out.println("encountered exception- " +throw1.getMessage()); } } }
Output