Why is invokeAll () not returning?

I have code like this:

ExecutorService threader = Executors.newFixedThreadPool(queue.size());
List futures = threader.invokeAll(queue);

      

I'm debugging this and invokeAll doesn't seem to return until all the threads in the queue have finished. Any reasons why this is happening.

+2


source to share


3 answers


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;
}

      

+6


source


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)

+5


source


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  
}

      

+2


source







All Articles