Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ExecutornewCachedThreadPoolExample2 { public static void main(final String[] arguments) throws InterruptedException { ThreadFactory ThreadFactory = Executors.defaultThreadFactory(); ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory); ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr; System.out.println("size of mypool: " + mypool.getPoolSize()); for(int i=0 ; i<= 4 ; i++) { excr.submit(new Threadimpl()); } excr.shutdown(); } static class Threadimpl implements Runnable { public void run() { try { Long num = (long) (Math.random() / 30); System.out.println("Thread Name: " +Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(num); System.out.println("after sleep Thread Name: " +Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Output