gpt4 book ai didi

java - 为什么 invokeAll() 不返回?

转载 作者:搜寻专家 更新时间:2023-11-01 04:05:21 25 4
gpt4 key购买 nike

我大概有这样的代码:

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

我对此进行了调试,直到队列中的所有线程都完成后,invokeAll 似乎才返回。发生这种情况的任何原因。

最佳答案

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

您必须一次一个地提交()它们,例如:

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

关于java - 为什么 invokeAll() 不返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com