一.异步任务
在开发中有时用户提交的数据,后台需要一定时间才能做出响应,此时用户在前台也不能在等待中,此时就应该先开启异步请求处理,利用多线程,先给前台反馈,后台另一线程去处理数据。
1.创建异步处理请求
package com.springboot.assigment.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @author panglili
* @create 2022-07-12-8:19
*/
//异步请求注解,表示此类开启异步请求
@Async
@Service
public class AsyncService {
public void Asycn(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中……");
}
}
此程序中,后台需要三秒等待才能处理好请求。
2.controller调用异步业务请求的方法
package com.springboot.assigment.controller;
import com.springboot.assigment.service