views:

165

answers:

2

I have roughly this code:

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

I debug this and invokeAll doesn't seem to return until all the threads in the Queue are finished. Any reasons why this is happening.

+3  A: 

Because it's designed like that:

[...]
Executes the given tasks, returning a list of Futures holding their status and results when all complete. 
[...]

is a quote from http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)

Johannes Weiß
+3  A: 

Executes the given tasks, returning a list of Futures holding their status and results when all complete. the API

You have to submit() them one at a time instead, 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;
}
Pete Kirkham