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 ThreadPoolExecuteallowsCoreThreadTimeOutExample1 { public static void main(String[] args) { //Creating a thread pool ThreadPoolExecutor obj = new ThreadPoolExecuteallowsCoreThreadTimeOutExample1().new MyThreadExecutor(2, 3, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); 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); if (this.allowsCoreThreadTimeOut()) { System.out.println("allowCoreThreadTimeOut is true"); this.allowCoreThreadTimeOut(true); } else System.out.println("allowCoreThreadTimeOut is false"); } } }
Output