[英]Java: ExecutorService with Callables: invokeAll() and future.get() - results in correct order?
[英]How to get the Future list in order when calling the executorService.invokeAll(callables)?
我hv下面的代碼
final ExecutorService executorService = Executors.newFixedThreadPool(ipAddressList.size());
final Set<Callable<JsonObject>> callables = new HashSet<Callable<JsonObject>>();
for (final String remoteHostName : ipAddressList)
{
callables.add(new Callable<JsonObject>() {
@Override
public JsonObject call()
throws ConnectionFailedException
{
return connectToHost(remoteHostName, options, attributes);
}
});
}
List<Future<JsonObject>> futures = null;
try
{
futures = executorService.invokeAll(callables);
}
catch (final InterruptedException e)
{...
}
for (final Future<JsonObject> future : futures)
{
..}
現在的問題是,異常可以在任何線程中發生,但是我沒有任何方法可以識別異常的IP地址。 有什么方法可以使期貨與可贖回證券的順序相同。
有什么想法嗎?
不能以與Callables相同的順序獲得期貨。.請注意,它在多線程環境中運行,並且沒有關於如何執行線程的特定順序。 我可以在這里想到兩種方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.