我对java.util.concurrent.Future还不熟悉,有一些问题。如果我使用Future调用一个服务,我怎么知道是哪个元素调用了这个服务?
这里有一个例子:
For each id I am using java.util.concurrent.Future to call a service to fill some extra 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)); ... }
for (Future<ObjectY> futureResult : future)
{
serviceResult.add(futureResult.get());
}
在这个阶段,我有一个结果列表,但我不知道哪个结果属于哪个ID。
ids.get(0).setResult(serviceResult.get(0))????
ids.get(0).setResult(serviceResult.get(1))????
ids.get(0).setResult(serviceResult.get(2))????
...
谢谢!