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 ThreadPoolExecutorgetCompletedTaskCountExample1 { public static void main(final String[] arguments) throws InterruptedException { ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); executor.submit(new Task()); executor.submit(new Task()); System.out.println("Completed task count : "+ executor.getCompletedTaskCount()); executor.shutdown(); } 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