Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.lang.Thread; import java.util.logging.Level; import java.util.logging.Logger; public class ThreadPoolExecutorsetCorePoolsizeExample2 { public static void main(String[] args) { Task8 task1 = new Task8("t1"); Task8 task2 = new Task8("t2"); System.out.println("Starting threads"); ThreadPoolExecutor tExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); tExecutor.execute(task1); tExecutor.execute(task2); // start task1 tExecutor.setCorePoolSize(5); System.out.println(" After execute Core pool size : "+ tExecutor.getCorePoolSize()); tExecutor.shutdown(); // shutdown worker threads System.out.println("Threads started, main ends\n"); } } class Task8 implements Runnable { private final String threadName; public Task8(String threadName) { this.threadName = threadName; } @Override public void run() { for(int i=0 ; i<=10;i++) { if(i<5) { try { System.out.println("Thread sleeping for 1000 i=" +i); Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(Task8.class.getName()).log(Level.SEVERE, null, ex); } } else{ System.out.println(" now i > 5 :"); break; } } } }
Output