Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how do I wait for all tasks, but halt on first error?

I have a series of concurrent tasks to run. If any one of them fails, I want to interrupt them all and await termination. But assuming none of them fail, I want to wait for all of them to finish.

ExecutorCompletionService seems like almost what I want here, but there doesn't appear to be a way to tell if all of my tasks are done, except by keeping a separate count of the number of tasks. (Note that both of the examples of in the Javadoc for ExecutorCompletionService keep track of the count "n" of the tasks, and use that to determine if the service is finished.)

Am I overlooking something, or do I really have to write this code myself?

like image 755
Dan Fabulich Avatar asked Sep 10 '09 01:09

Dan Fabulich


1 Answers

Yes, you do need to keep track if you're using an ExecutorCompletionService. Typically, you would call get() on the futures to see if an error occurred. Without iterating over the tasks, how else could you tell that one failed?

like image 96
Jeff Storey Avatar answered Oct 21 '22 13:10

Jeff Storey