定时任务Schedule多线程配置

本文介绍了在Spring项目中如何解决Schedule定时任务默认单线程导致的执行冲突问题。通过配置多线程,可以确保耗时任务与频繁任务并行执行。通过使用@Async注解实现异步执行,配合@Scheduled(cron = "0/9 * * * * ?")配置定时任务每9秒执行一次,避免了任务的阻塞。
摘要由CSDN通过智能技术生成

在项目过程中经常会用到spring的Schedule定时任务
然后Schedule定时任务默认是单线程的,在项目过程中就会出现比较耗时的任务跟比较频繁的任务冲突问题,

单线程情况下,例如8:00有一个耗时大约10分钟的定时任务,还有一个每一分钟一次的定时任务,那么8:00任务开始时,一分钟的任务就已经进入排队序列,并不执行

故而需要配置多线程的定时任务配置

配置文件: 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

im
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值