使用Spring的@Async创建异步方法

使用Spring的@Async创建异步方法

在开发系统的过程中,通常会考虑到系统的性能问题,提升系统性能的一个重要思想就是“串行”改“并行”。说起“并行”自然离不开“异步”,今天我们就来聊聊如何使用Spring的@Async的异步注解。

假设场景

你有一个很耗时的服务,我们在下面的例子中用线程休眠来模拟,服务执行需要5秒钟。假设一个请求需要调用这个服务3次,如果按照“串行”的方法,将至少需要15秒钟。那么为了提升系统的性能,我们采用“并行”的方法,我们最乐观的情况下,只需要5秒就可以了。有人可能会说这个很简单,我们写个多线程的方法就可以了。但是,今天我们看看Spring为我们提供的方法,它使得开发的过程更简单。

创建异步方法

首先,使用IDEA工具创建Spring-Boot项目,并且选择依赖包Lombok,具体步骤略。然后创建BusyService类,并创建busyMethod方法,具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
@Service
@Slf4j
public class BusyService {
 @Async
 public CompletableFuture<String> busyMethod(String name) throws InterruptedException {
 log.info(name);
 String s = "Hello,"+name+"!";
 //模拟耗时操作,5秒
 Thread.sleep(5000);
 return CompletableFuture.completedFuture(s);
 }
}

  

其中,BusyService上的注解@Service标识着它会被Spring初始化为一个实例,而@Slf4j则标识着我们可以直接使用log打印日志。然后我们再看看busyMethod方法,它的返回值是CompletableFuture,CompletableFuture继承自Future,它可以把多个异步执行的结果合并到一个单独的异步结果中,CompletableFuture是任何异步服务所需要的。我们再看看busyMethod方法上的注解@Async,这个注解是我们今天的主角,它标识着这个方法是异步方法,调用它时是异步调用的。再看看方法体中的内容,我们使用了线程休眠模拟那些耗时的服务,并返回CompletableFuture。

Executor线程池

我们在系统定义一个Executor的Bean,使得异步调用时,使用Executor线程池的线程去执行。这里为了方便,我们直接在Spring-Boot的启动类中增加这个Bean。

1
2
3
4
5
6
7
8
9
10
@Bean
public Executor taskExecutor() {
 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
 executor.setCorePoolSize(3);
 executor.setMaxPoolSize(3);
 executor.setQueueCapacity(500);
 executor.setThreadNamePrefix("Java同学会-");
 executor.initialize();
 return executor;
}

  

我们定义了最大的并发线程数为3,并且定义了队列中的最大任务数为500,线程名字的前缀为“Java同学会”,在log打印日志时,凡是线程池中的线程执行的,都会打印出“Java同学会”的线程名字。当然你还可以增加一些其他的设置。如果你不配置Executor这个Bean,Spring会自动创建SimpleAsyncTaskExecutor,并使用它来执行异步方法。

Controller

我们使用一个简单的RestController完成异步的调用,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@SpringBootApplication
@RestController
@EnableAsync
@Slf4j
public class SpringAsyncApplication {
 @Autowired
 private BusyService busyService;
 public static void main(String[] args) {
 SpringApplication.run(SpringAsyncApplication.class, args);
 }
 @RequestMapping("test")
 public String test() throws InterruptedException, ExecutionException {
 CompletableFuture<String> jane = busyService.busyMethod("Jane");
 CompletableFuture<String> allen = busyService.busyMethod("Allen");
 CompletableFuture<String> james = busyService.busyMethod("James");
 CompletableFuture.allOf(jane,allen,james).join();
 log.info(jane.get());
 log.info(allen.get());
 log.info(james.get());
 return "success";
 }
 @Bean
 public Executor taskExecutor() {
 ……
 }
}

  

我们在启动类上加上@EnableAsync注解,使得Spring-Boot可以使用异步调用。再看看test()方法,我们调用了3次异步方法,并等待它们全部完成后,将它们打印出来。我们启动项目,并在浏览器中访问这个方法,地址是:http://localhost:8080/test。

如何使用Spring的Async注解创建异步方法

 

我们在等待了5秒后,页面上返回了“success”。我们再看看后台打印的结果:

如何使用Spring的Async注解创建异步方法

 

我们看到名字前缀为“Java同学会”前缀的3个线程,打印了busyMethod方法中的日志。因为busyMethod方法是我们定义的Executor线程池中的线程执行的。我们再看看test方法和busyMethod方法中日志打印的时间,它们相隔了5秒,说明test方法中的CompletableFuture.allOf(jane,allen,james).join()一直在等待,等待所有调用的异步方法都执行完,才继续执行。

好了,Spring的@Async就介绍完了,是不是很方便呢?有问题评论区留言哦~~

 

posted @   牛初九  阅读(3013)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示