在 Spring Boot 中使用 @Async 注解中的 CompletableFuture.supplyAsync 和 CompletableFuture.completedFuture

mos*_*ini 5 java asynchronous future spring-boot completable-future

我有以下方法:

@EnableAsync
@Service
Class MyService{ 

private String processRequest() {
        log.info("Start processing request");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("Completed processing request");
        return RESULT;
    }    

@Async
public CompletableFuture<String> getSupplyAsyncResult(){
    CompletableFuture<String> future
            = CompletableFuture.supplyAsync(this::processRequest);
    return future;
}

@Async
public CompletableFuture<String> getCompletedFutureResult(){
    CompletableFuture<String> future
            = CompletableFuture.supplyAsync(this::processRequest);
    return future;
}
Run Code Online (Sandbox Code Playgroud)

以及控制器中的以下端点:

   @RequestMapping(path = "/asyncSupplyAsync", method = RequestMethod.GET)
    public CompletableFuture<String> getValueAsyncUsingCompletableFuture() {
        log.info("Request received");
        CompletableFuture<String> completableFuture
                = myService.getSupplyAsyncResult();
        log.info("Servlet thread released");
        return completableFuture;
    }
Run Code Online (Sandbox Code Playgroud)

和

   @RequestMapping(path = "/asyncCompletable", method = RequestMethod.GET)
    public CompletableFuture<String> getValueAsyncUsingCompletableFuture() {
        log.info("Request received");
        CompletableFuture<String> completableFuture
                = myService.getCompletedFutureResult();
        log.info("Servlet thread released");
        return completableFuture;
    }
Run Code Online (Sandbox Code Playgroud)

为什么有人会在 Spring 端点的 @Async 方法中使用completableFuture.supplyAsync ?我认为使用completableFuture.completedFuture 更合适,请分享您的观点。

Eug*_*ene 7

它们一开始就服务于完全不同的目的。在考虑其中一个或另一个需要多少时间来处理之前,您可能想首先了解它们是如何工作的(无论如何,很少的调用并不表示慢/快;这些数字在这种情况下没有任何意义)。

这是您拥有的相同示例:

public class SO64718973 {

    public static void main(String[] args) {
        System.out.println("dispatching to CF...");
        //CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> processRequest());
        CompletableFuture<String> future = CompletableFuture.completedFuture(processRequest());
        System.out.println("done dispatching to CF...");
        future.join();
    }

    private static String processRequest() {
        System.out.println("Start processing request");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("Completed processing request");
        return "RESULT";
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以运行它,然后更改实现(通过取消注释CompletableFuture.supplyAsync)并查看这些System.out.println发生的位置。您会注意到,completedFuture它将阻塞main线程直到它被执行,而supplyAsync将在不同的线程中运行。因此,并不是说一个是错的,一个是错的,这取决于您的用例。

CompletableFuture.supplyAsync一般来说,在不为其配置池的情况下使用并不是一个好主意;否则它将消耗来自 的线程ForkJoinPool。

  • @MortezaN 在这种情况下它是由主线程完成的。没有什么可以阻止您或其他任何人共享此“CompletableFuture”,以便其他线程可以完成它。 (2认同)