Skip to content
Advertisement

Executor Service – InvokeAll: How to Map response?

I am working on parallel execution of 5 Tasks with Executor service, all the 5 tasks returns back with different object results-set depending upon the task.

I am using executor service for parallel execution of tasks :

List<Future<Object>> taskResults = ex.invokeAll(callables);

But how do we map the result set back with the task(s) ? It may not return back the response in the same order of callable list?

Advertisement

Answer

I found that invokeAll() sends back the response of tasks in sequential order which can be used for mapping response back to task ( callables ).

To quote the Javadoc for ExecutorService#invokeAll:

Returns:

a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed

Source : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#invokeAll-java.util.Collection-

Answer : ThreadPoolExcutor and invokeAll list order

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement