【多线程编程】--任务调度@Scheduled、ScheduledThreadPoolExecutor、quartz、xxl-job

本文对比分析了SpringBoot中四种任务调度方式:@Scheduled注解、ScheduledThreadPoolExecutor、Quartz和XXL-Job。@Scheduled为单线程,可能存在任务阻塞问题;ScheduledThreadPoolExecutor适用于随机触发场景,但任务异常处理受限;Quartz可灵活控制任务,但不适合分布式;XXL-Job则适合分布式调度,但配置相对复杂。
摘要由CSDN通过智能技术生成

说明
定时任务调度在很多场景中应用,并且市场上也有很多技术栈(如@Scheduled、ScheduledThreadPoolExecutor、quartz等),下面就以Springboot集成这些定时任务调度技术进行对比,比较在实际应用中的优劣。

一、springboot集成@Scheduled注解

1.1、集成@Scheduled注解方法

要想开启@Scheduled,只需要Application启动类增加@EnableScheduling注解即可。

@SpringBootApplication(scanBasePackages= {
   "com.wwy"})
@EnableScheduling  //开启定时任务功能
public class LearnitemApplication extends SpringBootServletInitializer {
   
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
   
        return builder.sources(LearnitemApplication.class);
    }
    public static void main(String[] args) {
   
        SpringApplication.run(LearnitemApplication.class, args);
    }
}

具体使用过程

@Component
public class ScheduleTaskServer {
   

    /**
     * cron 每10秒执行
     */
    @Scheduled(cron = "0/1 * * * * *")
    public void cronV1(){
   
        System.out.println("cronV1 "+Thread.currentThread().getName()+" 执行时间start:"+new Date());
        try {
   
            Thread.sleep(5000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "0/1 * * * * *")
    public void cronV2(){
   
        System.out.println("cronV2 "+Thread.currentThread().getName()+" 执行时间start:"+new Date());
    }
 }

结果:

 * cronV2 scheduling-1 执行时间start:Mon Jan 10 11:18:35 CST 2022
     * cronV1 scheduling-1 执行时间start:Mon Jan 10 11:18:35 CST 2022
     * cronV2 scheduling-1 执行时间start:Mon Jan 10 11:18:40 CST 2022
     * cronV1 scheduling-1 执行时间start:Mon Jan 10 11:18:41 CST 2022
     * cronV2 scheduling-1 执行时间start:Mon Jan 10 11:18:46 CST 2022
     * cronV1 scheduling-1 执行时间start:Mon Jan 10 11:18:47 CST 2022
     * cronV2 scheduling-1 执行时间start:Mon Jan 10 11:18:52 CST 2022
     * cronV1 scheduling-1 执行时间start:Mon Jan 10 11:18:53 CST 2022

结果分析:
即使开启两个@Scheduled,也是同一个线程【scheduling-1】执行,说明@Scheduled注解是单线程。
本身 cronV2先执行完,然后等待cronV1执行完,才能执行cronV2。

1.2、集成@Scheduled注解优劣点

存在问题:
(1)、单线程执行,如果某个任务阻塞会影响其他任务;
(2)、某个任务失败,后续还会继续执行,并且任务随着系统服务启动而开启,不能终止或中途添加;
(3)、只能适用于单机服务架构,不适合分布式场景;

1.3、集成@Scheduled注解改进

针对上面第一个存在问题,可以使用多线程来解决。--------------采用多线程解决方法有两种:(1)、通过配置设置Scheduled为多线程;(2)、自己维护线程池。

通过配置设置Scheduled为多线程

@Configuration
public class ScheduleConfiguration implements SchedulingConfigurer {
   
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
   
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
    }
}

自己维护线程池

@Component
public class AsyncScheduleTaskServer {
   

    private static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
            2,2,1000,TimeUnit.SECONDS,new LinkedBlockingDeque<>(100));

    @Scheduled(cron = "0/1 * * * * *")
    public void asyncCronV1(){
   
        poolExecutor.execute(()->{
   
            System.out.println("async  CronV1 "+Thread.currentThread().getName()+" 执行时间start:"+new Date());
            try {
   
                Thread.sleep(3000);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
        });
    }

    @Scheduled(cron = "0/1 * * * * *")
    public void asyncCronV2(){
   
        poolExecutor.execute(()->{
   
            System.out.println("async  CronV2 "+Thread.currentThread().getName()
在 Spring Boot 中,我们可以使用 `@Scheduled` 注解来创建定时任务。该注解可以用于方法上,表示这个方法是一个定时任务。在方法上加上该注解后,Spring Boot 会自动创建一个定时任务,并按照指定的时间间隔执行该方法。 下面是一个简单的示例: ```java @Component public class MyTask { @Scheduled(fixedRate = 5000) public void run() { System.out.println("Hello, world!"); } } ``` 上面的代码中,我们定义了一个名为 `MyTask` 的类,它被标注为 `@Component`,表示这是一个组件类。该类内部还有一个名为 `run` 的方法,它被标注为 `@Scheduled(fixedRate = 5000)`,表示该方法是一个定时任务,每 5 秒钟执行一次。 除了 `fixedRate` 属性外,`@Scheduled` 注解还有其他许多属性可供配置,例如: - `fixedDelay`:表示上一次任务执行完后延迟多长时间再执行下一次任务。 - `initialDelay`:表示首次任务执行前延迟多长时间。 - `cron`:支持使用 cron 表达式来定制更复杂的执行时间规则。 下面是一个带有 `fixedDelay` 和 `initialDelay` 属性的定时任务示例: ```java @Component public class MyTask { @Scheduled(fixedDelay = 5000, initialDelay = 1000) public void run() { System.out.println("Hello, world!"); } } ``` 上面的代码中,我们将 `fixedDelay` 属性设置为 5000 毫秒,表示上一次任务执行完后延迟 5 秒钟再执行下一次任务;将 `initialDelay` 属性设置为 1000 毫秒,表示首次任务执行前延迟 1 秒钟。 最后,还要注意一点,使用 `@Scheduled` 注解创建的定时任务默认是单线程的,如果任务执行时间过长,会阻塞整个应用程序的运行。因此,在实际应用中,我们需要根据实际情况来控制任务的执行时间,或者使用线程池等机制来保证任务的并发执行。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DreamBoy_W.W.Y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值