我在java中有一個List<List<String>>
,我想在父列表內異步處理List,並使用固定線程池示例3.我正嘗試在java 8中使用CompletableFuture和Stream。我不理解如何合併這兩個以及如何繼續。 PFB代碼我已經嘗試過。在處理器中,我只是打印它,但我會做數據庫操作。如何在java中使用CompletableFuture處理List列表?
所以在這裏我想要流List<List<String>>
並創建基於列表大小的線程數量,但鋤頭將流列表作爲參數傳遞給Processor with CompletableFuture。
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;
}
}
'未來 F = excecutor.submit(() - > PROCESSLIST(列表))'每個列表? –
assylias
你可以編寫簡單的程序,它有一個線程(簡單調試),並運行該程序使用執行外部進程Apache Commons Exec。 – Grzesiek