簡體   English   中英

調用executorService.invokeAll(callables)時如何按順序獲取Future列表?

[英]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相同的順序獲得期貨。.請注意,它在多線程環境中運行,並且沒有關於如何執行線程的特定順序。 我可以在這里想到兩種方法:

  1. IP地址是異常消息的一部分,您可以從那里提取
  2. Yon可以在Callable中捕獲異常並將其包裝為結果的一部分。 也許您可以為此使用javalangs?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM