The problem with this is that, since the method "process()" takes quite a while to complete with 10 threads, the tt.shutdownService() got immediately executed. How to ensure the shutdownService() is executed after other threads, i.e. after the process() is executed?
public class ThreadTest{
private ExecturoService service;
void main(String[] args){
long startTime = System.currentTimeLillis();
ThreadTest tt = new ThreadTest();
tt.process();
tt.shutdownService();
long endTime = System.currentTimeLillis();
}
public ThreadTest(){
service = Executors.newFixedThreadPool(10);
}
public void process(){
...
service.execute(new Runnable){...}
}
public void shutdownService(){
if(service != null){
service.shutdown();
service.awaitTermination(60, TimeUnit.SECONDS);
}
}
}
Note, my actual program is a little more complex. I can't shutdown the service in process(), since process() will be called multiple times in a loop. Here I simplified the case.