Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.*; import java.lang.Thread; import java.util.logging.Level; import java.util.logging.Logger; class MYThreadPoolExecutor7 extends ThreadPoolExecutor { public MYThreadPoolExecutor7() { super(1,10,60,TimeUnit.SECONDS,new ArrayBlockingQueue
(1000)); } @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); if (t == null && r instanceof Future>) { try { Object Obj = ((Future>) r).get(); System.out.println(Obj); } catch (InterruptedException | ExecutionException e) { } } } } public class ThreadPoolExecuteawaitTerminationExample1 { public static void main(String args[]) throws InterruptedException{ System.out.println("creating service"); MYThreadPoolExecutor7 obj2 = new MYThreadPoolExecutor7(); Future> sb = obj2.submit(() -> { try { obj2.awaitTermination(10, TimeUnit.SECONDS); //await termination method } catch (InterruptedException ex) { Logger.getLogger(ThreadPoolExecuteawaitTerminationExample1.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("await termination for thread:"+Thread.currentThread().getName()); }); obj2.shutdown(); } }
Output