1

Ich habe eine List<List<String>> in Java, ich möchte die Liste innerhalb der Elternliste Liste Asynchron mit festen Thread-Pool Beispiel 3. Ich versuche CompletableFuture und Stream in Java 8. Ich verstehe nicht, wie diese beiden zusammenführen und wie Sie vorgehen . PFB Code habe ich bisher ausprobiert. Im Prozessor drucke ich es nur, aber ich werde DB-Operationen machen.Wie Liste der Liste mit CompleableFuture in Java verarbeiten?

Also hier versuche ich Stream List<List<String>> und Anzahl der Thread basierend auf der Größe der Liste zu erstellen, sondern hoe übergeben Sie die gestreamte Liste als Argument Prozessor mit CompleableFuture.

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

Warum sind Sie eine CompletableFuture verwenden und nicht einfach Aufruf 'Zukunft f = excecutor.submit (() -> PROCESS (Liste))' für jede Liste? – assylias

+0

Sie können das einfache Programm schreiben, das einen Thread hat (einfach zu debuggen) und führt dieses Programm aus, das externe Prozesse Apache Commons Exec ausführt. – Grzesiek

Antwort

1

Also von dem, was ich verstehe, was Sie brauchen Ihren Prozessor für jede Ihrer List<String> in Ihrem List<List<String>>

So nennen, was Sie tun können, ist alles von neuen Threads erstellen mit CompletableFuture dann auf sie warten alle auf Beenden Sie und führen Sie eine Verarbeitung der zurückgegebenen Werte durch.

Also, was Sie tun können, ist so etwas wie dieses

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

dies, wie Sie Listen der Liste und completablefuture fusionieren können.

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; 
    } 
} 
Verwandte Themen