Online Java Compiler By
JavaTpoint.com
package ThreadPoolExecutorClass; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPoolExecutorbeforeExecuteExample1 { public static void main(String[] args) { //Creating a thread pool ThreadPoolExecutor obj = new ThreadPoolExecutorbeforeExecuteExample1().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 beforeExecute(Thread thread, Runnable run) { super.beforeExecute(thread, run); System.out.println(thread.getName() + "it will run the task."); } } }
Output