赞
踩
1.首先去主类上开启@EnableAsync
- @SpringBootApplication
- @EnableScheduling
- @EnableAsync //开启异步
- public class NginxApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NginxApplication.class, args);
- }
-
- }
2.新建一个类,添加@Async注解
- @Service
- public class Testasync {
-
-
- @Async
- public void async1() {
-
- System.out.println("async1异步id:"+Thread.currentThread().getId());
- }
-
- @Async
- public void async2() {
- System.out.println("async2异步id:"+Thread.currentThread().getId());
- }
- }
3.调用此方法
- @Controller
- @RequestMapping("/test")
- public class zhu {
-
- @Autowired
- private Testasync async;
-
- @RequestMapping("/async")
- @ResponseBody
- public String test() {
-
- System.out.println("Thread id:"+Thread.currentThread().getId());
- async.async1();
- async.async2();
- return "进入了异步方法";
-
- }
-
- }
打印出显示出不同的线程id:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。