首页  

springboot异步处理 @Async 注解     所属分类 springboot 浏览量 91
springboot 启动类加上 @EnableAsync 注解
@Async才会生效

@Async注解 标注在方法上 或 类上
调用异步方法时会立即返回结果,方法的实际执行将提交给Spring TaskExecutor 线程池执行。

在启动类上添加@EnableAsync注解

不指定线程池的名称,使用默认的线程池 SimpleAsyncTaskExecutor

@Async注解的方法的返回值只能是void或者Future类型


默认线程池的默认配置
核心线程数 8
最大线程数 Integet.MAX_VALUE
队列  LinkedBlockingQueue
容量 Integet.MAX_VALUE
空闲线程保留时间 60s
拒绝策略 AbortPolicy


spring:
  task:
    execution:
      pool:
        max-size: 10
        core-size: 5
        keep-alive: 2s
        queue-capacity: 1000
        thread-name-prefix: my-executor


指定线程池 @Async("my-executor") @Bean("async-executor") public Executor asyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(10); taskExecutor.setMaxPoolSize(100); taskExecutor.setQueueCapacity(50); taskExecutor.setKeepAliveSeconds(200); taskExecutor.setThreadNamePrefix("async-executor-"); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.initialize(); return taskExecutor; }

上一篇     下一篇
springboot 配置文件 bootstrap 与 application

Linux su命令

物联网六大核心技术

JDK 和 openJDK 区别

Flink CDC 3.0 简介

Skywalking简介