1.异步任务
多线程实现:
1.创建模块springboot-12-async
2.创建service:异步任务的服务
/**
* 异步任务
*/
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("后台数据正在进行处理-----");
}
}
3.创建控制器:AsyncController
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "ok";
}
}
测试:
页面会等待3秒后才响应
4.将service中的方法,设置为 异步调用方法,使用@Async注解
@Async // 告诉spring这是一个异步调用方法
public void hello(){
try {
Thread.sleep(3000);