With invokeAll you can also achieve.
List<Future<byte[]>> results = exec.invokeAll(callables);
It will return a list of future objects.
ThreadPoll will execute the tasks in random order. The task execution order cannot be monitored.
Future object
His link to the task, which is presented in ThreadPool.
It has a get () method. Its a call blocking method. If you call the get () method, the call will wait until the tasks are completed.
How to ensure the order of sending tasks
The order of the results is the same in the order of treatment. Even the thread pool will execute in a different order, which we can get in the same order.
For example
Three tasks 1,2,3
1 and 3 → Completed.
2 ------> Takes a lot of time.
After calling invokeAll, it will return a list of the future in the same order.
Task 1: Future # get returns the result of the return from the moment it is completed
Task 2: the future # get method waits for the task to complete.
The iteration awaits the completion of the second task, although the third task is completed. He will not receive a result from the third stream.
source share