likes
comments
collection
share

SpringBoot执行异步任务Async介绍

作者站长头像
站长
· 阅读数 29

前言

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

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

使用场景

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

使用示例

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

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

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

    @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. 在其他需要调用的地方,将这个异步方法所在的类进行注入,然后调用

    @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执行异步任务Async介绍

自定义异步调用的线程池

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

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

  1. 自定义异步线程池

    @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名称。

    @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. 调用自定义的异步方法

    asyncComponent.selfAsync();
    
  4. 执行结果

    SpringBoot执行异步任务Async介绍

Async失效场景(注意事项)

  1. 调用方法和异步方法在同一个类中,会导致Async失效。
  2. 异步方法使用了static进行修饰,会导致Async失效。
转载自:https://juejin.cn/post/7277830326691840059
评论
请登录