目录
异步任务
@Async
@EnableAsync //开启异步注解支持
方法上面加一个注解 @Async
AsyncService
@Service
public class AsyncService {
//告诉Spring 这是一个异步的方法 Application 需要开启 @EnableAsync
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理....");
}
}
main 上面加一个注解 @EnableAsync
Application
@SpringBootApplication
@EnableAsync //开启异步注解支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
AsyncController
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello(); //停止3秒,转圈
return "ok";
}
}