JUC(八)--定时任务

时间:2025-05-08 07:51:25

Timer的缺陷

在java中,Timer类可以实现定时任务,但Timer有两个致命缺陷,一是timer的任务是单线程运行的,如果前面的任务运行时间过长,会影响后面任务的运行,二是如果任务发生异常没有捕获,则后面的任务就不会执行了。

定时线程池

使用定时线程池则可以解决以上的问题

延迟任务

//创建两个线程的定时线程池
ScheduledExecutorService scheduledExecutorService = (2);
//任务延迟1S执行
(() -> ("任务执行了11"), 1, );
(() -> ("任务执行了22"), 1, );

输出,可以看到有两个线程在执行任务

14:43:39.796 [pool-1-thread-2] INFO  - 任务执行了22
14:43:39.796 [pool-1-thread-1] INFO  - 任务执行了11

定时任务

scheduleAtFixedRate表示定时间隔执行,而跟前面任务的执行时间没关系(前提是前面任务的执行时间不能超过定时时间),比如间隔2S,那执行时间就是1S,3S,5S这样

//创建两个线程的定时线程池
ScheduledExecutorService scheduledExecutorService = (2);
//定时任务,初始延迟1S,每隔2S执行一次
(() -> {
    ("执行任务1");
    try {
        (1000);
    } catch (InterruptedException e) {
        ();
    }
}, 1, 2, );
("start");

scheduleWithFixedDelay是前面的任务执行完了后面的任务才会放入队列中,比如任务执行时间是1S,间隔2S,那执行时间是1S,4S,7S 

//创建两个线程的定时线程池
ScheduledExecutorService scheduledExecutorService = (2);
//定时任务,初始延迟1S,前面任务执行完了再隔2S执行一次
(() -> {
    ("执行任务1");
    try {
        (1000);
    } catch (InterruptedException e) {
        ();
    }
}, 1, 2, );
("start");

SpringBoot集成定时任务

第一步,开启定时任务

@EnableScheduling
public class ServerApp {

第二步,给定时任务加注解

fixedRate表示任务间隔N秒执行,fixedDelay表示前面的任务执行完了再间隔N秒执行

@Scheduled(fixedRate = 1000)
private void schedule() {
    ("hello");
}

@Scheduled(fixedDelay = 1000)
private void schedule() {
    ("hello");
}

Scheduled注解还支持cron表达式,记不住cron表达式也没关系,通过这个网站可以生成quartz/Cron/Crontab表达式在线生成工具-

@Scheduled(cron = "0/1 * * * * ?")
private void testCron() {
    ("hello");
}

 需要注意的是,springboot的定时任务都是单线程的,也就是一个任务如果太耗时,会影响其他任务的执行,通过自定义线程池来实现多线程

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //根据业务设置核心线程数
        ((2));
    }
}