0

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 ?

Thanks

xain
  • 13,159
  • 17
  • 75
  • 119
  • possible duplicate of [Is there a way to access an iteration-counter in Java's for-each loop?](http://stackoverflow.com/questions/477550/is-there-a-way-to-access-an-iteration-counter-in-javas-for-each-loop) – Brian Roach Apr 15 '11 at 18:48

2 Answers2

0

see the solution here

you have to implement your own counter to know what index your are up to.

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
0

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 (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++);
}
mdma
  • 56,943
  • 12
  • 94
  • 128