I have a list of callable objects , which I am using to generate a list of future objects. I am using ExecutorService to get the Callable tasks done concurrently and storing the result as list of Future objects. Now I want to know, is there a way so I can get the original callable object which was used to generate a given future object.
Asked
Active
Viewed 1,361 times
2
-
I have a little difficulty understanding the question. So you would want to know the Callable object that you have used to invoke the executor service? Is it the T in Callable
or the callable object in List – vijayakumarpsg587 Dec 31 '18 at 09:40.?
2 Answers
1
If you obtain your Future
s by calling ExecutorService.invokeAll()
you can rely on the order as is documented in the javadoc.
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list, each of which has completed

Frank Neblung
- 3,047
- 17
- 34
-
1So I am creating two separate callables and added them to callableList. After that I called, executorService.invokeall(cllableList) which returns list of Future Objects. But, I am getting the output of the callbles at random, even though the first future object should have returned the output of first callable. Can you please explain. – Lazycoder-007 Oct 09 '19 at 12:21
0
The default implementation doesn't give you this functionality though you can create your own callable
import org.apache.commons.math3.util.Pair;
class MyCallable implements Callable<Pair<Callable,MyObject>>{
private MyService service;
public String name;
public MyCallable(MyService srevice,String name){
this.srevice = srevice;
this.name = name;
}
@Override
public Pair<Callable,MyObject> call() throws Exception {
return new Pair(this,service.doSomeStuff())
}
}
Now when you invoke the callable object in your future, you will have access to the callable object. Something like future.get().getFirst().name
will work.

Reza
- 1,516
- 14
- 23