Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.*; class MYThreadPoolExecutor3 extends ThreadPoolExecutor { public MYThreadPoolExecutor3() { super(1,10,60,TimeUnit.SECONDS,new ArrayBlockingQueue
(1000)); this.allowCoreThreadTimeOut(false); } @Override protected void beforeExecute(Thread thread, Runnable run){ super.beforeExecute(thread, run); this.allowCoreThreadTimeOut(true); try { Object Obj = ((Future>) run).get(); System.out.println(Obj); } catch (InterruptedException | ExecutionException e) { } } } public class ThreadPoolExecuteallowCoreThreadTimeOutExample2 { public static void main(String args[]){ System.out.println("creating service"); MYThreadPoolExecutor3 obj2 = new MYThreadPoolExecutor3(); Future> sb = obj2.submit(() -> { int a=4, b = 0; System.out.println("a and b="+a+":"+b); System.out.println("a/b:"+(a/b)); System.out.println("Thread Name in Runnable after divide by zero:"+Thread.currentThread().getName()); }); obj2.shutdown(); } }
Output