开发者

Get Futures' IDs in Java

开发者 https://www.devze.com 2023-02-26 18:36 出处:网络
I have the following code: for (int i = 0; i < nComp; i++) { Ca开发者_JAVA技巧llable<Long> worker = new WSCaller(compConns[i]);

I have the following code:

for (int i = 0; i < nComp; i++) {
    Ca开发者_JAVA技巧llable<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


see the solution here

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


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++);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消