使用方法

  1. 在启动类或 Controller 加注解
@EnableAsync
  1. 在需要异步执行的方法加注解
@Async

配置线程池

@Async 默认异步配置使用的是 SimpleAsyncTaskExecutor,该线程池默认给一个任务创建一个线程,若系统中不断的创建线程,最终会导致系统占用内存过高,引发 OOM 。

这时,我们可以定义一个线程池,异步方法将会被自动提交到线程池中执行

  1. 注册自定义的 ThreadPoolTaskExecutor
@EnableAsync
@Configuration
public class TaskPoolConfig {
 
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程池大小
        executor.setCorePoolSize(10);
        //最大线程数
        executor.setMaxPoolSize(20);
        //队列容量
        executor.setQueueCapacity(200);
        //活跃时间
        executor.setKeepAliveSeconds(60);
        //线程名字前缀
        executor.setThreadNamePrefix("taskExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
  1. @Async 指定线程池
@Async("taskExecutor")
  1. 可以在 AsyncExecutionAspectSupport#determineAsyncExecutor 看到,线程池变成了 ThreadPoolTaskExecutor 类型

参考链接