异步任务
提前返回结果,中间需要执行的耗时过程,重开一条线程去执行。
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
//告诉Spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中....");
}
@SpringBootApplication
@EnableAsync //开启异步的功能
public class AsytaskmailApplication {
public static void main(String[] args) {
SpringApplication.run(AsytaskmailApplication.class, args);
}
}
参考目录
非常详细的 笔记
https://blog.csdn.net/weixin_44449838/article/details/108732490
狂神
https://www.bilibili.com/video/BV1PE411i7CV?p=49