zoukankan      html  css  js  c++  java
  • SpringBoot-@async异步执行方法

    启动加上@EnableAsync ,需要执行异步方法上加入  @Async

    在方法上加上@Async之后 底层使用多线程技术

    演示代码:

    @RestController
    @Slf4j
    public class IndexController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("/index")
        public String index() {
            log.info("##01##");
            userService.userThread();
            log.info("##04##");
            return "success";
        }
    
    }
    
    @Service
    @Slf4j
    public class UserService {
    
        @Async // 类似与开启线程执行..
        public void userThread() {
            log.info("##02##");
            try {
                Thread.sleep(5 * 1000);
            } catch (Exception e) {
                // TODO: handle exception
            }
            log.info("##03##");
    
            // new Thread(new Runnable() {
            // public void run() {
            // log.info("##02##");
            // try {
            // Thread.sleep(5 * 1000);
            // } catch (Exception e) {
            // // TODO: handle exception
            // }
            // log.info("##03##");
            // }
            // }).start();
        }
    
    }
    
    
    @EnableAsync // 开启异步注解
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }
  • 相关阅读:
    Linux内核的异常修复原理
    sudo: insmod: command not found
    在Qemu+ARM上运行Minix3内核
    2021.34 面对干扰
    2021.33 实践
    selenium+python自动化106
    python测试开发django-111.模型管理器(models.Manager)
    python笔记64
    python笔记63
    python笔记62
  • 原文地址:https://www.cnblogs.com/XJJD/p/10394368.html
Copyright © 2011-2022 走看看