aspose file tools*
The moose likes Threads and Synchronization and the fly likes Using ExecutorService Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Using ExecutorService" Watch "Using ExecutorService" New topic
Author

Using ExecutorService

Sven Anderson
Ranch Hand

Joined: Apr 14, 2004
Posts: 58
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

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Apr 14, 2004
Posts: 58
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using ExecutorService