ExecutorService.invokeAll不支持可运行任务的收集 [英] ExecutorService.invokeAll does NOT support collection of runnable task
本文介绍了ExecutorService.invokeAll不支持可运行任务的收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
希望通过 ExecutorService 。
但是现在不支持(仅支持可调用任务的集合)
这有什么特别的原因吗?做类似事情的替代方法是什么。
Any specific reason for this? What's the alternative to do something similar.
推荐答案
只需将runnable转换为callables:
Simply transform the runnables into callables:
List<Callable<Void>> callables = new ArrayList<>();
for (Runnable r : runnables) {
callables.add(toCallable(r));
}
executor.invokeAll(callables);
private Callable<Void> toCallable(final Runnable runnable) {
return new Callable<Void>() {
@Override
public Void call() {
runnable.run();
return null;
}
};
}
这篇关于ExecutorService.invokeAll不支持可运行任务的收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文