Why is invokeAll () not returning?
Performs the specified tasks, returning a list of futures keeping their status and results when completed. API
You need submit()
them one at a time, and something like:
public static <T> List<Future<T>> submitAll ( ExecutorService executor, Collection<? extends Callable<T> > tasks ) {
List<Future<T>> result = new ArrayList<Future<T>>( tasks.size() );
for ( Callable<T> task : tasks )
result.add ( executor.submit ( task ) );
return result;
}
source to share
Because it is created like this:
[...]
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
[...]
- this is a quote from http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)
source to share
When we call a method invokeAll()
, it returns an object List<Futute<T>>
. And this object Future
retains the value of all threads until it succeeds. So when we try to iterate through an object Future<T>
, it first checks inside Future<T>.isDone()
[We may or may not check Future<T>.isDone()
from outside]. If it returns true, we can iterate through the object and access the object's value Future<T>
, otherwise it will expect true.
Note. If it Future<T>
receives a false object, it will prevent you from iterating over the object.
ExecutorService threader = Executors.newFixedThreadPool(queue.size());
List<Future<T>> listFuture = threader.invokeAll(queue);
for(Future<T> future: listFuture) {
// Here you have the full access to this future object
}
source to share