jdk定时任务的使用

文章对比了Java的Timer与ScheduledExecutorService在定时任务调度中的优缺点,推荐使用ScheduledExecutorService以实现并发、灵活的定时任务执行,避免Timer的单线程限制和异常处理问题。
摘要由CSDN通过智能技术生成

Timer

JDK内置了 java.util.Timer 类,

可以用来调度 java.util.TimerTask 任务。

可以前端页面传递定时执行的简单时间

几个重要的方法:

schedule:开始调度任务,提供了几个包装方法;
cancle:终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响;
purge:从任务队列中移除所有已取消的任务;
另外,java.util.TimerTask 就是实现了 Runnable 接口,具体任务逻辑则是在 run 方法里去实现。

private static void timerTask() throws InterruptedException {
        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("hi, 欢迎关注:Java技术栈");
            }
        };

        // 第一次任务延迟时间
        long delay = 2000;

        // 任务执行频率
        long period = 3 * 1000;

        // 开始调度
        timer.schedule(timerTask, delay, period);

        // 指定首次运行时间
// timer.schedule(timerTask, DateUtils.addSeconds(new Date(), 5), period);

        Thread.sleep(20000);

        // 终止并移除任务
        timer.cancel();
        timer.purge();
    }

这种实现方式比较简单,可以指定首次执行的延迟时间、首次执行的具体日期时间,以及执行频率,能满足日常需要。

另外,需要注意的是,Timer 是线程安全的,因为背后是单线程在执行所有任务。

Timer 也会有一些缺陷:

Timer 是单线程的,假如有任务 A,B,C,任务 A 如果执行时间比较长,那么就会影响任务 B,C 的启动和执行时间,如果 B,C 执行时间也比较长,那就会相互影响;
Timer 不会捕获异常,如果 A,B,C 任何一个任务在执行过程中发生异常,就会导致 TImer 整个定时任务停止工作;
Timer 是基于绝对时间调度的,而不是基于相对时间,所以它对系统时间的改变非常敏感;
所以,如果在使用 Timer 的过程中要注意这些缺陷,虽然可以用,但不推荐。

ScheduledExecutorService

因 Timer 有一些缺陷,所以不太建议使用 Timer,推荐使用 ScheduledExecutorService:

/**
 * Creates a thread pool that can schedule commands to run after a
 * given delay, or to execute periodically.
 * @param corePoolSize the number of threads to keep in the pool,
 * even if they are idle
 * @return a newly created scheduled thread pool
 * @throws IllegalArgumentException if {@code corePoolSize < 0}
 */
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

/**
 * Creates a thread pool that can schedule commands to run after a
 * given delay, or to execute periodically.
 * @param corePoolSize the number of threads to keep in the pool,
 * even if they are idle
 * @param threadFactory the factory to use when the executor
 * creates a new thread
 * @return a newly created scheduled thread pool
 * @throws IllegalArgumentException if {@code corePoolSize < 0}
 * @throws NullPointerException if threadFactory is null
 */
public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}

上了线程池,每个调度任务都会分配到线程池中的某一个线程去执行,任务就是并发调度执行的,任务之间互不影响。

几个重要的调度方法:

schedule:只执行一次调度;
scheduleAtFixedRate:按固定频率调度,如果执行时间过长,下一次调度会延迟,不会同时执行;
scheduleWithFixedDelay:延迟调度,上一次执行完再加上延迟时间后执行;
另外,可以看出,任务是支持 Runnable 和 Callable 调度的。

/**
 * 线程池定时任务
 * 这是一个按固定频率调度的任务,创建了 10 个核心线程数,首次执行延迟 2 秒,后续每 3 秒执行一次。
 * 这种方式简单、好用,避免了使用 Timer 带来的各种问题,推荐使用这种实现方式。
 */
 public static void poolTask(){
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);

        pool.scheduleAtFixedRate(() -> {
            System.out.println("2 秒后开始执行,每 3 秒执行一次");
        }, 2000, 3000, TimeUnit.MILLISECONDS);

        pool.schedule(() -> {
            System.out.println("2 秒后开始执行,我只执行一次");
        }, 2000, TimeUnit.MILLISECONDS);

        pool.scheduleWithFixedDelay(() -> {
            System.out.println("hi?????");
        }, 2000, 3000,TimeUnit.MILLISECONDS);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
定时任务是指在预定的时间间隔或特定时间执行的任务。而分布式定时任务是指将这些任务分布到多台机器上执行,以实现更高的可靠性和可伸缩性。 Java中有多种实现定时任务和分布式定时任务的方法,其中一种是使用JDK原生提供的定时任务功能。通过使用`java.util.Timer`或`java.util.concurrent.ScheduledExecutorService`类,可以在Java中创建和调度定时任务。 另一种常见的实现方式是使用Spring框架。Spring提供了丰富的定时任务支持,包括基于注解的定时任务和基于XML配置的定时任务。通过使用`@Scheduled`注解,可以将方法标记为定时任务,并指定任务的执行时间间隔或特定时间点。 此外,Spring还提供了整合数据库和Redis的方式来存储和管理定时任务。通过将任务列表存储在关系型数据库或Redis中,可以实现任务的持久化和分布式管理。 对于分布式定时任务,可以使用消息队列(如RabbitMQ)来实现任务的分发和调度。通过将任务发布到消息队列中,不同的任务消费者可以从队列中获取任务并执行。这种方式可以实现任务的水平扩展和负载均衡。 另外,还有一些开源的分布式定时任务框架,如Quartz、Elastic-Job、XXL-Job等,它们提供了更丰富的功能和更灵活的配置选项。 总结起来,定时任务可以通过JDK原生的定时任务、Spring框架、数据库或Redis存储以及消息队列来实现。而分布式定时任务则可以通过使用消息队列和开源框架来实现。具体选择哪种方式取决于项目需求和技术栈的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

研程序笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值