用Java获取Futures的ID

时间:2011-04-15 18:39:34

标签: java multithreading future

我有以下代码:

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

正如代码中所指出的...我怎么知道哪个未来超时?

由于

2 个答案:

答案 0 :(得分:0)

查看解决方案here

你必须实施自己的计数器才能知道你的目标是什么。

答案 1 :(得分:0)

期货的退回顺序与提交的可赎回方式相同,因此可赎回名单和期货清单中的指数之间存在一对一的对应关系。

您可以使用传统的for循环,

for (int i=0; i<results.size(); i++) {
   Future<Long> future = results.get(i);
   Callable<Long> callable = col.get(i);
}

或维护索引,

int index = 0;
for (Future<Long> f: results) {
   Callable<Long> c = col.get(index++);
}
相关问题