gpt4 book ai didi

java - 有没有办法像在 ExecutorCompletionService 中那样在 spring 中轮询一组 Future 对象?

转载 作者:行者123 更新时间:2023-11-30 11:52:53 25 4
gpt4 key购买 nike

我正在使用 @Async我的一个函数上的注释,该函数返回 Future<integer>目的。该方法被调用几次,结果放在一个列表中。有没有办法以类似于 ExecutorCompletionService.take() 的方式轮询该列表?有用吗?

因为我使用的是 spring,所以我没有传递给 ExecutorCompletionService ctor 的执行器。

最佳答案

好吧,我还没有找到字符串提供的东西,所以我实现了自己的东西(非常欢迎评论):

public class CompletionService<V> {

private List<Future<V>> results;
private static boolean loop = true;

public CompletionService(List<Future<V>> results) {
this.results = results;
}

public Future<V> take() {

Future<V> retval = null;
int i = -1;

while (loop) {
for (i = 0; i < results.size(); i++) {
if (results.get(i).isDone()) {
retval = results.get(i);
abortTake();
break;
}
}
}

//remove task only if it is done
if (i > -1 && results.get(i).isDone()) {
results.remove(i);
}
resetLoop();
return retval;
}

public synchronized void abortTake() {
loop = false;
}

private synchronized void resetLoop() {
loop = true;
}

}

关于java - 有没有办法像在 ExecutorCompletionService 中那样在 spring 中轮询一组 Future 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612471/

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