2017-10-05 5 views
-3

私はこのようなことを達成しようとしています。これは意図を表現した構成例です。ループ内の完成可能な未来を呼び出し、すべての結果を組み合わせるにはどうすればいいですか?

私はすべての完成可能な未来を実行して、すべての結果を1つの結果にまとめて返したいと思います。したがって、以下の例では、コレクションallResultsは "1"、 "2"、 "3"、それぞれ3回の文字列を持つ必要があります。私は彼ら全員が連続していなくても並行して走って欲しいです。

これを達成するためにどのようなAPIを使用することができるかについては、非常に参考になります。それは、前のタスクの結果を待っている間、あなたも他のタスクを開始していない意味するだろうと

public class Main { 


    public static void main(String[] args) { 

     int x = 3; 
     List<String> allResuts; 

     for (int i = 0; i < x; i++) { 

      //call getCompletableFutureResult() and combine all the results 
     } 

    } 

    public static CompletableFuture<List<String>> getCompletableFutureResult() { 

     return CompletableFuture.supplyAsync(() -> getResult()); 
    } 

    private static List<String> getResult() { 


     List<String> list = new ArrayList<>(); 
     list.add("one"); 
     list.add("two"); 
     list.add("three"); 

     return list; 
    } 


} 
+0

_combine_を定義します。 –

答えて

1

あなたは、第一forループで結果を収集することはできません。

すべてのタスクが開始されたら結果を収集し始めます。

public static void main(String[] args) throws Exception 
{ 
    int x = 3; 

    Queue<CompletableFuture<List<String>>> cfs = new ArrayDeque<>(x); 
    for (int i = 0; i < x; i++) 
    { 
    cfs.add(getCompletableFutureResult()); 
    } 

    List<String> allResuts = new ArrayList<>(); 
    for (CompletableFuture<List<String>> cf : cfs) 
    allResuts.addAll(cf.get()); 

    System.out.println(allResuts); 
} 
1

Venkata Rajuの回答に問題があります。 Rajuはを使用してを呼び出します。これはブロッキングコールであり、非同期スタイルのコーディングの主な目的を奪います。常に先物取引を避けてください。

方法で構築されたのトンはthenCombineなどを使用すると、複数の先物に対処する必要がありますときにメソッドが使用されることを意図してCompletableFuture.allOf

、thenApply、thenAccept、thenComposeなどの将来の値を扱う周りの構築があります。

それは、この次のシグネチャ

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) 

サイドノートました:あなたが唯一の完了するために最初の将来を気にする場合CompletableFuture.anyOfを使用することができます。すべての先物が完了する必要がある場合は、allOfを使用してください。

CompletableFuture.allOfを使用して、次のように仕様をコーディングします。

public class DorjeeTest { 


    public static CompletableFuture<List<String>> getCompetableFutureResult() { 
     return CompletableFuture.supplyAsync(() -> getResult()); 
    } 
    public static List<String> getResult() { 
     return Lists.newArrayList("one", "two", "three"); 
    } 

    public static void testFutures() { 
     int x = 3; 
     List<CompletableFuture<List<String>>> futureResultList = Lists.newArrayList(); 
     for (int i = 0; i < x; i++) { 
      futureResultList.add(getCompetableFutureResult()); 
     } 

     CompletableFuture[] futureResultArray = futureResultList.toArray(new CompletableFuture[futureResultList.size()]); 

     CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(futureResultArray); 

     CompletableFuture<List<List<String>>> finalResults = combinedFuture 
       .thenApply(voidd -> 
         futureResultList.stream() 
           .map(future -> future.join()) 
         .collect(Collectors.toList())); 

     finalResults.thenAccept(result -> System.out.println(result)); 
    } 


    public static void main(String[] args) { 
     testFutures(); 
     System.out.println("put debug break point on this line..."); 

    } 
} 
関連する問題