1

私はList<List<String>>をJavaで持っています。親リスト内のリストを非同期的に固定スレッドプールの例3で処理します。CompletableFutureとStreamをJava 8で使用しようとしています。これらの2つをマージする方法と、 。 PFBコード私はこれまでに試したことがあります。プロセッサーでは印刷していますが、私はDB操作を行います。javaのCompletableFutureでリストのリストを処理するには?

ここでストリームList<List<String>>を作成しようとしていますが、リストサイズに基づいてスレッド数を作成していますが、hoeはStreamed ListをCompletableFutureのProcessorに引数として渡します。

public class CompletableFutureWithList { 
    public static void main(String args[]) { 
     List<List<String>> aList = new ArrayList<>(); 
     aList.add(new ArrayList<>(Arrays.asList("xyz", "abc"))); 
     aList.add(new ArrayList<>(Arrays.asList("qwe", "poi"))); 
     System.out.println("helo..."); 
     ExecutorService executor = Executors.newFixedThreadPool(aList.size()); 
     //aList.stream().flatMap(List::stream). 
     Processor aProcessor = new Processor(); 
     List<String> tempList = new ArrayList<>(); 
     CompletableFuture aComFuture = supplyAsync(() -> aProcessor.processList(tempList), executor); 
     try { 
      aComFuture.get(); 
     } catch (InterruptedException | ExecutionException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
public class Processor { 
    public boolean processList(List<String> tempList) { 
     for (String string : tempList) { 
      System.out.println("Output: " + string); 
     } 
     return true; 
    } 
} 
+2

なぜあなたはCompletableFutureを使用していて、単純に呼び出していない '将来 F = excecutor.submit(() - > PROCESSLIST(リスト))'各リストのために? – assylias

+0

1つのスレッド(デバッグが簡単)を持つシンプルなプログラムを作成し、このプログラムを使用して外部プロセスの実行に使用することができます。Apache Commons Exec。 – Grzesiek

答えて

1

は、だから私はあなたが何ができるかだからあなたList<List<String>>

の内側にあなたのList<String>ごとに、あなたのプロセッサを呼び出す必要があります理解して何から、その後にそれらすべてを待つCompletableFutureを使用して、新しいスレッドをすべて作成することです終了し、返された値の処理を行います。

だから何あなたができることは、これはあなたがリストとcompletablefutureのリストをマージすることができる方法である。この

List<List<String>> aList = new ArrayList<>(); 

//Create all CFs 
List<CompletableFuture<Boolean>> futureList = aList.stream() 
      .map(strings -> CompletableFuture.supplyAsync(() -> processList(strings), executor)) 
      .collect(toList()); 

//Wait for them all to complete 
CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join(); 

//Do processing of the results 
Stream<Boolean> booleanStream = futureList.stream() 
      .map(CompletableFuture::join); 
//Do other stuff you need 
0

のようなものです。

public static void main(String args[]) { 
    List<List<String>> aList = new ArrayList<>(); 
    aList.add(new ArrayList<>(Arrays.asList("xyz", "abc"))); 
    aList.add(new ArrayList<>(Arrays.asList("qwe", "poi"))); 
    System.out.println("hello..."); 

    Processor aProcessor = new Processor(); 
    List<String> tempList = new ArrayList<>(); 
    CompletableFuture aComFuture = CompletableFuture.supplyAsync(() -> ""); 

    aList.stream() 
      .forEach(list -> aComFuture.thenApply(fn -> aProcessor.processList(list))); 

    aComFuture.join(); 
} 

static class Processor { 
    public boolean processList(List<String> tempList) { 
     for (String string : tempList) { 
      System.out.println("Output: " + string); 
     } 
     return true; 
    } 
} 
関連する問題