通过流并行执行多个查询

通过流并行执行多个查询,第1张

通过流并行执行多个查询

您可以利用

CompletableFuture
这种方式:

public String getResult() {    // Create Stream of tasks:    Stream<Supplier<List<String>>> tasks = Stream.of( () -> getServerListFromDB(), () -> getAppListFromDB(), () -> getUserFromDB());    List<List<String>> lists = tasks         // Supply all the tasks for execution and collect CompletableFutures         .map(CompletableFuture::supplyAsync).collect(Collectors.toList())         // Join all the CompletableFutures to gather the results         .stream()         .map(CompletableFuture::join).collect(Collectors.toList());    // Use the results. They are guaranteed to be ordered in the same way as the tasks    return getResult(lists.get(0), lists.get(1), lists.get(2));}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5016822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存