Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.lang.Thread; public class ThreadPoolExecutorisShutdownNowExample1 { public static void main(final String[] arguments) throws InterruptedException { ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); executor.submit(new Task()); System.out.println(" Is Executor is shutdown :"+ executor.isShutdown()); executor.submit(new Task()); executor.shutdownNow(); System.out.println("get Task count after:"+ executor.getTaskCount()); System.out.println(" Is Executor is shutdown :"+ executor.isShutdown()); } static class Task implements Runnable { @Override public void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Thread Name: " + Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(duration); System.out.println("Completed Thread Name: " +Thread.currentThread().getName()); } catch (InterruptedException e) { } } } }
Output