SpringBoot执行异步任务Async介绍

前言

本篇文章的代码示例已放到 github 上,Git地址为:advance(记录每一个学习过程),大家把代码下载下来之后,全局搜索一些关键代码,即可找到该文章的源码。

大家觉得有用的话,麻烦点个star👍再走呗!

使用场景

当我们在使用SpringBoot进行开发的时候,可能会遇到一些执行异步任务的场景,如果每次执行这些异步任务都去新建一个异步线程来执行的话,那代码就太冗余了。幸好SpringBoot给我们提供了Async的注解,让我们能够很轻松地对这些异步任务进行执行。

使用示例

  1. 在启动类上使用@EnableAsync注解,表示开启异步任务

    less 复制代码
    @EnableAsync
    @SpringBootApplication
    public class AsycnDemoApplication {
    ​
        public static void main(String[] args) {
            SpringApplication.run(AsycnDemoApplication.class, args);
        }
    ​
    }
  2. 将需要执行的异步方法所在的类,加入到Spring的容器中,可以使用@Component注解

    kotlin 复制代码
    @Component
    public class AsyncComponent {
    ​
    }
  3. 在需要异步执行的方法上,加入@Async注解

    csharp 复制代码
    @Component
    public class AsyncComponent {
        @Async
        public void async(String str){
            System.out.println("输入的内容是" + str + ",异步任务正在休眠5秒..");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                System.out.println("休眠失败");
            }
            System.out.println("输入的内容是" + str + ",异步任务执行结束");
        }
    }
  4. 在其他需要调用的地方,将这个异步方法所在的类进行注入,然后调用

    java 复制代码
    @Component
    public class LineRunner implements CommandLineRunner {
        @Autowired
        private AsyncComponent asyncComponent;
    ​
        @Override
        public void run(String... args) throws Exception {
            System.out.println("主线程开始");
            asyncComponent.async("今天不上班,好耶");
            asyncComponent.selfAsync();
            System.out.println("主线程结束");
        }
    }
  5. 执行结果

自定义异步调用的线程池

SpringBoot默认会使用SimpleAsyncTaskExecutor线程池,这个不是真的线程池,不会重用线程,每次调用都会新建一个线程出来,用完之后就回收掉,没起到重复利用的作用。并发量太大的话,可能会有内存溢出的风险。

因此,更加推荐开发者对异步调用的线程池进行自定义。

  1. 自定义异步线程池

    scss 复制代码
    @Configuration
    public class ExecutorsAsyncConfig {
        @Bean(name = "asyncConfig")
        public Executor asyncConfig(){
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            //设置核心线程数
            executor.setCorePoolSize(5);
            //设置最大线程数
            executor.setMaxPoolSize(50);
            //设置缓存的队列
            executor.setQueueCapacity(1000);
            //设置空闲线程的超时时间
            executor.setKeepAliveSeconds(1000 * 5);
            //设置线程名称的前缀
            executor.setThreadNamePrefix("async-config-");
            executor.initialize();
            return executor;
        }
    }
  2. 编写自定义的异步方法,其实也就就是在@Async的注解上加了线程池的bean名称。

    csharp 复制代码
    @Async("asyncConfig")
    public void selfAsync(){
        System.out.println("我是自定义异步线程,线程池名称:" + Thread.currentThread().getName());
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            System.out.println("休眠失败");
        }
        System.out.println("自定义异步线程休眠结束");
    }
  3. 调用自定义的异步方法

    ini 复制代码
    asyncComponent.selfAsync();
  4. 执行结果

Async失效场景(注意事项)

  1. 调用方法和异步方法在同一个类中,会导致Async失效。
  2. 异步方法使用了static进行修饰,会导致Async失效。
相关推荐
我叫啥都行几秒前
计算机基础知识复习9.7
运维·服务器·网络·笔记·后端
工业互联网专业9 分钟前
毕业设计选题:基于springboot+vue+uniapp的驾校报名小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
无名指的等待7121 小时前
SpringBoot中使用ElasticSearch
java·spring boot·后端
.生产的驴1 小时前
SpringBoot 消息队列RabbitMQ 消费者确认机制 失败重试机制
java·spring boot·分布式·后端·rabbitmq·java-rabbitmq
AskHarries2 小时前
Spring Boot利用dag加速Spring beans初始化
java·spring boot·后端
苹果酱05672 小时前
一文读懂SpringCLoud
java·开发语言·spring boot·后端·中间件
掐指一算乀缺钱3 小时前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
飞翔的佩奇3 小时前
xxl-job适配sqlite本地数据库及mysql数据库。可根据配置指定使用哪种数据库。
数据库·spring boot·mysql·sqlite·xxl-job·任务调度
计算机学姐5 小时前
基于python+django+vue的影视推荐系统
开发语言·vue.js·后端·python·mysql·django·intellij-idea
JustinNeil5 小时前
简化Java对象转换:高效实现大对象的Entity、VO、DTO互转与代码优化
后端