[英]Get Futures' IDs in Java
I have the following code: 我有以下代码:
for (int i = 0; i < nComp; i++) {
Callable<Long> worker = new WSCaller(compConns[i]);
col.add(worker);
}
List<Future<Long>> results=null;
results = executor.invokeAll(col, timeout, TimeUnit.SECONDS);
for (Future<Long> future : results) {
if ( !future.isDone() ) {
// here I need to know which future timed-out ...
}
}
As pointed out in the code ... How can I know which Future timed-out ? 如代码中所指出的...我如何知道哪个Future超时?
Thanks 谢谢
The futures are returned in the same order as the submitted callables, so there is a one to one correspondence between the indices in the callables list and futures list. 期货以与提交的可赎回债券相同的顺序返回,因此可赎回债券列表和期货列表中的索引之间存在一对一的对应关系。
You can either use a traditional for loop, 您可以使用传统的for循环,
for (int i=0; i<results.size(); i++) {
Future<Long> future = results.get(i);
Callable<Long> callable = col.get(i);
}
or maintain an index, 或维持索引
int index = 0;
for (Future<Long> f: results) {
Callable<Long> c = col.get(index++);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.