为什么invokeAll()不会返回?

ari*_*nte 3 java concurrency

我大致有这个代码:

ExecutorService threader = Executors.newFixedThreadPool(queue.size());
List futures = threader.invokeAll(queue);
Run Code Online (Sandbox Code Playgroud)

我调试了这个,并且在队列中的所有线程都完成之前,invokeAll似乎没有返回.发生这种情况的任何原因.

Pet*_*ham 7

执行给定的任务,返回完成所有状态和结果的Futures列表.API

你需要submit()一次一个,比如:

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;
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*iss 6

因为它是这样设计的:

[...]
Executes the given tasks, returning a list of Futures holding their status and results when all complete. 
[...]
Run Code Online (Sandbox Code Playgroud)

引自http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)