我在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