Scheduled定时任务异步执行

1.使用配置

我在使用SpringBoot配置定时任务的过程中,使用@Scheduled配置了多个定时任务,但是在项目启动的时候每次只会启动一个定时任务,只好搜索一波,直到看到了 ThreadPoolTaskScheduler的源码,才发现默认开启的线程数是 1 

@Configuration
public class ScheduledPoolConfig implements SchedulingConfigurer {


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(4);
        taskRegistrar.setScheduler(scheduledExecutorService);
    }
}

 创建一个线程为四个的线程池

2.使用@Async注解

1.创建线程池

@SpringBootConfiguration
public class SpringAsyncConfig implements AsyncConfigurer {
	 
	 @Bean("taskExecutor")
     public Executor taskExecutor() {
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
         executor.setCorePoolSize(80);
         executor.setMaxPoolSize(80);
         executor.setQueueCapacity(1000);
         executor.setKeepAliveSeconds(60);
         executor.setThreadNamePrefix("taskExecutor-");
         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
         return executor;
     }


}

 主要参数

一、corePoolSize 线程池核心线程大小

线程池中会维护一个最小的线程数量,即使这些线程处理空闲状态,他们也不会被销毁,除非设置了allowCoreThreadTimeOut。这里的最小线程数量即是corePoolSize。任务提交到线程池后,首先会检查当前线程数是否达到了corePoolSize,如果没有达到的话,则会创建一个新线程来处理这个任务。

二、maximumPoolSize 线程池最大线程数量

当前线程数达到corePoolSize后,如果继续有任务被提交到线程池,会将任务缓存到工作队列(后面会介绍)中。如果队列也已满,则会去创建一个新线程来出来这个处理。线程池不会无限制的去创建新线程,它会有一个最大线程数量的限制,这个数量即由maximunPoolSize指定。

三、keepAliveTime 空闲线程存活时间

一个线程如果处于空闲状态,并且当前的线程数量大于corePoolSize,那么在指定时间后,这个空闲线程会被销毁,这里的指定时间由keepAliveTime来设定

四、unit 空闲线程存活时间单位

keepAliveTime的计量单位

2.主启动类添加@EnableAsync注解开启异步

@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
@EnableTransactionManagement
@ComponentScan("cn.com.geostar")
@MapperScan("cn.com.geostar.external.mapper")
@ServletComponentScan
@EnableAsync
@EnableScheduling
@EnableDiscoveryClient
@EnableCaching
public class SpringBootStartApplication extends SpringBootServletInitializer {

	public static void main(String[] args) { SpringApplication.run(SpringBootStartApplication.class, args); }

}

在主启动类上添加@EnableAsync注解代表开启异步,如不开启的话,则无法进行异步,此处相当于开关

3.在定时任务中添加注解@Async 

在定时任务上面添加注解@Async注解,注解中的value代表线程池,如果没有自定义的线程池,可以无需填写,系统回使用默认的线程池使用,使用自定义线程池是为了方便管理 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨会停rain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值