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 ThreadPoolExecuteallowCoreThreadTimeOutExample1 { public static void main(String[] args) { //Creating a thread pool ThreadPoolExecutor obj = new ThreadPoolExecuteallowCoreThreadTimeOutExample1().new MyThreadExecutor(2, 3, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); // obj.allowCoreThreadTimeOut(); //execute a runnable obj.execute(() -> { System.out.println("In Runnable!"); }); obj.shutdown(); } class MyThreadExecutor extends ThreadPoolExecutor { private final boolean allowCoreThreadTimeOut = false; public MyThreadExecutor(int PoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue
workQueue) { super(PoolSize, maxPoolSize, keepAliveTime, unit, workQueue); this.allowCoreThreadTimeOut(false); if (this.allowCoreThreadTimeOut) this.allowCoreThreadTimeOut(true); else System.out.println("allowCoreThreadTimeOut is already true"); } } }
Output