This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Using ExecutorService

 
Sven Anderson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got the following question about using the ExecutorService.

With the following code snippet. Can I make the assumption that all threads have completed once I get to
populate the List? Or could it be a case where a thread is still running once I start adding elements to the list?

I'm a bit confused here

Thanks
S


 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven: Can I make the assumption that all threads have completed once I get to
populate the List?


Yes!

Actually, the line:



ensures that all the tasks that you submitted would have completed.
The reason being, get() waits for task completion. So, there is no way(apart from throwing an exception like InterruptedException/CancellationException) that get() call can return without task completion. Since this line is executed only after the other 3 have finished and your executor is a local variable(hope it does not escape the method) so you can safely assume that all tasks have finished execution.

Just FYI, calling shutdown does not guarantee that all tasks have finished execution when the method returns.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Nitesh, just to be sure though, you need to make sure the futures you are getting are the ones returned from the submit:


I assume that is the case, but just wanted to be sure.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
I agree with Nitesh, just to be sure though, you need to make sure the futures you are getting are the ones returned from the submit:


I assume that is the case, but just wanted to be sure.


Good catch Steve, I did not notice that
 
Sven Anderson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies guys!

If I was to implement an ExecutorCompletionService class for the example above. Would I gain anything in terms of performance by doing this? Or is it simply a matter of writing neater code.

THanks
S
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you will gain some performance, because you won't have to wait for all the tasks to be done before adding them to the list (and/or doing whatever other work you need done with the results). By using the take() method you know you have a finished value, you can add it to the list, do your work then come back to the CompletionService when you are ready for the next one while the other tasks are still executing and finishing up.

If all you are doing is adding to the list and you need all of them finished before you move on then the amount of saved time will be tiny unless you have many concurrent activities.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic