import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExecutorTest {
public static void main(String[] args) {
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
myTask task = new myTask(i);
fixedThreadPool.submit(task);
}
fixedThreadPool.shutdown();
}
}
class myTask implements Runnable{
public myTask(int index){
this.index=index;
}
private int index;
@Override
public void run() {
try {
System.out.println(index);
if(index==9){
System.out.println("waiting for index 9");
Thread.sleep(10000);
System.out.println("completed for index 9");
}else{
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}