我大致有这个代码:
ExecutorService threader = Executors.newFixedThreadPool(queue.size());
List futures = threader.invokeAll(queue);
Run Code Online (Sandbox Code Playgroud)
我调试了这个,并且在队列中的所有线程都完成之前,invokeAll似乎没有返回.发生这种情况的任何原因.
执行给定的任务,返回完成所有状态和结果的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)
因为它是这样设计的:
[...]
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
[...]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |