• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print out statement after executing to ThreadPoolExecutor?  RSS feed

 
Joshua Rodrigues
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make a statement of elapsed time print out after I've executed everything with a ThreadPoolExecutor. What happens is that instead of printing out after all the iterations within the for each loop are done, it prints out the elapsed statement in the middle of the iterations or even in the beginning of the whole execute() method. Here is my code:


I hope I made it clear as to what I wish to achieve here! Thanks!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The threads in the thread pool run completely independently to the thread that is running the main method. Your main method thread adds all of the runnables to the thread pool, and then immediately prints the elapsed time. You have just timed how long it takes to create and register the runnables.

What you need to do is keep a reference to the Futures that gets returned when you register the runnables, and then call join on them. This will force the main thread to wait until the Runnables are complete.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!