I want to find the elapsed time for my multithreaded program.I have used ThreadPoolExecutor to manage threads.When I am not using the Thread pools,I used to use join() in the main method,so that my main thread will not terminate before the child threads terminate and hence I am able to find elapsed time.But when I use Thread pool ,even if I use join(),the main thread terminates before the child threads,so I am unable to calculate elapsed time.
I have attached a part of the code for your reference.Please help me.
I tried using a 'for' loop after pool.shutdown() instead of the pool.awaitTermination(, , )
But it did not work for me,the main thread terminates before the other threads.
Your help is appreciated.
ThreadPoolExecutor also has a method named 'isTerminated()'. You could put an awaitTemination(...) with a relatively small timeout into a while loop that checks isTerminated(), such that you continue to accumulate time until the ThreadPoolExecutor actually ends.
Or you could extend ThreadPoolExecutor and implement the timing mechanism in it, using the protected method terminated() to report back either that it is done, or to report how long it ran.
Joined: Mar 22, 2010
Thank you Steve for your guidance.The while loop worked perfectly.