zoukankan      html  css  js  c++  java
  • SpringBoot实现异步

    1、创建AsyncTest类

    package com.cppdy.service;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    @Component
    public class AsyncTest {
        
        @Async
        public void asyncOut() {
            System.out.println("异步方法id:"+Thread.currentThread().getId());
        }
    
    }

    2、在UserController中创建测试方法

    @RequestMapping("async")
        public String async() {
            System.out.println("Main Thread Id:"+Thread.currentThread().getId());
            asyncTest.asyncOut();
            return "async";
        }

    3、在Application类中开启异步(@EnableAsync)

    package com.cppdy;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    @SpringBootApplication
    @EnableAsync
    public class Application {
        
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
  • 相关阅读:
    Python 15 爬虫(一)
    Python 14 Mysql数据库(二)
    Python 13 JQuery&Bootstrp
    Python 12 CSS&JavaScript&DOOM
    Python 11 HTML
    Python 10 MySQL数据库(一)
    Python 9 Redis
    Python 8 协程
    Python 7 并发编程
    SNMP协议详解
  • 原文地址:https://www.cnblogs.com/jiefu/p/10051968.html
Copyright © 2011-2022 走看看