Reputation: 79
I need to time the total time it takes for a method to complete, usually i use:
long startTime = System.currentTimeMillis();
MethodWithThreads(); // the method which uses threading
long endTime = System.currentTimeMillis();
total = endTime - startTime;
Now obiviously the problem is that the mainthread terminates before the rest of the threads. Inside the thread function is the following code:
int cores = Runtime.getRuntime().availableProcessors();
ExecutorService pool = Executors.newFixedThreadPool(cores-1);
for(int i = 0; i < 1000; i++)
{
pool.submit(new RunnableThread());
}
pool.shutdown();
So how am i going to find the total time for the thread method to finish?
Upvotes: 0
Views: 144
Reputation: 79
Stupid me... should have done more research. I used the isTerminated in a while loop and awaited termination.
while(!pool.isTerminated())
{
try {
pool.awaitTermination(1000, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
Logger.getLogger(ThreadManagement.class.getName()).log(Level.SEVERE,null, ex);
}
}
long endTime = System.currentTimeMillis();
Upvotes: 1