What advantage is there to using Spring @Async vs. CompleteableFuture directly?

前端 未结 2 1182
夕颜
夕颜 2021-02-04 01:45

What\'s the advantage of using Spring Async vs. Just returning the CompletableFuture on your own?

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 02:47

    Your application is managed by the container. Since it's discouraged to spawn Threads on you own, you can let the container inject a managed Executor.

    @Service
    class MyService {
      @Autowired
      private Executor executor;
    
      public CompletableFuture compute() {
        return CompletableFuture.supplyAsync(() -> /* compute value */, executor);
      }
    }
    

提交回复
热议问题