2011-04-15 52 views
0

我有以下代码:获取期货的ID在Java中

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

如上代码中指出...我怎样才能知道哪些未来超时?

感谢

+0

可能重复环?](http://stackoverflow.com/questions/477550/is-there-a-way-to-access-an-iteration-counter-in-javas-for-each-loop) – 2011-04-15 18:48:11

回答

0

看到的解决办法here

,你必须实现自己的柜台知道你是到什么指标。

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++); 
} 
的[是否有访问Java对,每个迭代柜台的方式