0
I HV下面的代码如何在调用executorService.invokeAll(callables)时按顺序获取Future列表?
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地址外的任何方法来识别。有没有什么办法可以按照与可召集的顺序相同的顺序来获得期货。
有什么想法?
甚至不要尝试像依赖订单那样做一些脆弱而愚蠢的事情,但在例外中包含地址,例如?请注意,您正在使用'HashSet',所以您没有订单。 – Kayaman
虽然它可能不是*指定*,我认为这个顺序可能是相同的(如果你使用'List'而不是'HashSet',这将更容易检查)。但我一般都认为,除了短时间,高度可控的本地调试以外,您不应该依赖此类功能。 – Marco13
@Kayaman但是我从第三方APIC返回通用ExecutionException,我无法控制其添加IP地址 – abyin007