Reputation: 415
try {
for (final Future<Boolean> bool : threadPool.invokeAll(tasks)) {
if (!bool.get()) {
return false;
}
}
return true;
} finally {
threadPool.shutdownNow();
threadPool.awaitTermination(
IMPORT_THREADS_AWAIT_TERMINATION_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
}
Assume the task can throw an exception. Say there are 5 tasks, and the 5th task throws an exception, but the 1st task in the loop returns false. ( and loop breaks). If that task throws an exception, I want to catch it and do some cleanup logic. I am wondering my logic will never capture exception thrown by task 5 since i break from the loop on task 1
In such a case, will we ever encounter exception thrown by task 5 ?
Show threadPool.shutdownNow() throw the exception of task 5 ?
Upvotes: 2
Views: 52
Reputation: 28269
The exception
will be thrown by task5
, but the executor
will catch it and wrap it as ExecutionException
.
In this case, unless you call futureOfTask5.get()
, the exception
will not be re-thrown.
You can add try...catch
block in the call
method of task5
to log it.
Upvotes: 3