直接使用Spring @Async与CompleteableFuture有什么优势? [英] What advantage is there to using Spring @Async vs. CompleteableFuture directly?

查看:217
本文介绍了直接使用Spring @Async与CompleteableFuture有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Spring异步和仅返回CompletableFuture靠你自己?

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

推荐答案

您的应用程序由容器管理.由于不建议自己生成Thread,因此可以让容器注入托管的Executor.

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);
  }
}

这篇关于直接使用Spring @Async与CompleteableFuture有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆