I am new to java.util.concurrent.Future and ask a few questions. If I call a service using Future, how do I know which element was used to call the service?
Here is an example:
For each identifier, I use java.util.concurrent.Future to call the service to populate some additional data.
Collection< Future< ObjectX>> future = new ArrayList< Future< ObjectX>>();
Edit ###
List< ObjectY> serviceResult= new ArrayList< ObjectY>(); for (ObjectX obj: ids) { future.add(getAsyncInfo(obj); } //Because I have a lot of ids i need to call the service @async @Async public Future< ObjectY> getAsyncInfo(ObjectX obj){ return new AsyncResult<ObjectY>(callService(obj)); ... }
Get an answer
for (Future<ObjectY> futureResult : future) { serviceResult.add(futureResult.get()); }
At this point, I have a list of results, and I donβt know which result belongs to what id
ids.get(0).setResult(serviceResult.get(0))???? ids.get(0).setResult(serviceResult.get(1))???? ids.get(0).setResult(serviceResult.get(2))???? ...
Thanks!
source share