简体   繁体   English

如何使用 CompletableFuture 将此代码转换为 Java 8 中的异步代码? 我正在使用 Spring 引导

[英]How to convert this code to async in Java 8 using CompletableFuture ? I am using Spring Boot

Here is the Code Snippet that needs to be made async:这是需要异步的代码片段:

List<Course> courses = Lists.newArrayList();
Map<String, Map<String, Student>> result = Maps.newHashMap();

for (Map<String, Object> map : jsonObject) {
   courses.add(getCourse(map, fee));  
   result.put(map.get(GROUP), getInfoMap(map));  
}

PS: the map in the for-each loop is not mutated in both the method calls, only the information is used by them. PS: for-each循环中的map在两个方法调用中都没有发生突变,它们仅使用信息。

How can I make the above code run async?如何使上述代码异步运行? There are two methods being called getCourse(map, fee) and getInfoMap(map) and it takes quite a time to run them sequentially.有两种方法被称为getCourse(map, fee)getInfoMap(map) ,顺序运行它们需要相当长的时间。

    // start parallel operations
    List<Future<Course>> coursesFut = Lists.newArrayList();
    Map<String, Future<Map<String, Student>>> resultsFut = Maps.newHashMap();
    for (Map<String, Object> map : jsonObjec t){
        coursesFut.add(CompletableFuture.supplyAsync(() -> getCourse(map, fee)));
        resultsFut.put(map.get(GROUP), CompletableFuture.supplyAsync(() -> getInfoMap(map)));
    }

    // collect results
    List<Course> courses = Lists.newArrayList();
    Map<String, Map<String, Student>> result = Maps.newHashMap();
    for (Future<Course> cf : coursesFut) {
        courses.add(cf.get());
    }
    for (Map.Entry<String, Future<Map<String, Student>> entry : resultsFut.entrySet()) {
        result.put(entry.getKey(), entry.getValue().get());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将CompletableFuture与@Async一起使用将为Spring Boot API返回空响应 - Using CompletableFuture with @Async returns an empty response for spring boot API 如何在Spring Boot中使用CompletableFuture并行调用方法多次 - How to call a method multiple times in parallel using CompletableFuture in Spring Boot 使用Java Spring Boot,如何将依赖项,源代码和Spring合并到一个Jar文件中 - Using Java Spring Boot, How do I combine the dependencies, my source code, and Spring into one Jar file 如何在电子邮件中附加图像? 我正在使用 AWS SES 服务使用 JAVA 发送电子邮件 - Spring Boot - How do i attach image in email? I am using AWS SES Service to send email using JAVA - Spring Boot 我正在尝试将 gRPC 协议的指标传递给 kubernetes istio。 我正在使用 java spring 启动 - I am trying to pass the metric of a gRPC protocol to kubernetes istio. I am using java spring boot 如何在运行时使用 spring 引导应用程序中的 java 代码对 application.yaml 进行读写操作? - How can I write and read to and from a application.yaml at runtime using java code in a spring boot application? Java 8 - 如何使用 CompletableFuture 跟踪异步并行流中调用的异常数 - Java 8 - How to track number of exceptions invoked within an async parallel stream using CompletableFuture 使用Spring Boot在Java中发送异步HTTP请求 - Sending Async HTTP requests in Java using Spring Boot 如何转换代码以使用CompletableFuture? - How to convert the code to use CompletableFuture? 如何将此代码从Spring转换为Spring Boot? - How do I convert this code from Spring to Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM