Input
Output
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPool { public static void main(String[] args) { // creates four tasks Runnable r1 = new Task("task 1"); Runnable r2 = new Task("task 2"); Runnable r3 = new Task("task 3"); Runnable r4 = new Task("task 4"); // creates a thread pool and threads as the fixed pool size ExecutorService es = Executors.newFixedThreadPool(2); // passes the Task objects to the pool to execute es.execute(r1); es.execute(r2); es.execute(r3); es.execute(r4); // pool shutdown es.shutdown(); } } class Task implements Runnable { private String name; public Task(String s) { name = s; } public void run() { try { for (int i = 0; i<=2; i++) { if (i==0) { System.out.println("Initialization "+Thread.currentThread().getName() + " task name - "+ name +" " ); //prints the Thread initialization for every task } else { System.out.println("Executing "+Thread.currentThread().getName() +" task name - "+ name +" " ); // prints the Thread execution for every task } Thread.sleep(1000); } System.out.println(name+" complete"); } catch(InterruptedException e) { e.printStackTrace(); } } }