Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find elapsed time for ThreadPool

 
Sify Alto
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
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.

Sify
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sify Alto
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Steve for your guidance.The while loop worked perfectly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic