面试突击34:如何使用线程池执行定时任务?

在 Java 语言中,有两个线程池可以执行定时任务:ScheduledThreadPool 和
SingleThreadScheduledExecutor,其中
SingleThreadScheduledExecutor 可以看做是 ScheduledThreadPool 的单线程版本,它的用法和 ScheduledThreadPool 是一样的,所以本文重点来看 ScheduledThreadPool 线程池的使用。

ScheduledThreadPool 执行定时任务的方法有以下 3 个:

  1. 使用 schedule 方法执行定时任务,只执行一次定时任务。
  2. 使用 scheduleAtFixedRate 方法执行定时任务,执行多次定时任务。
  3. 使用 scheduleWithFixedDelay 方法执行定时任务,执行多次定时任务。

接下来我们看这 3 个方法的具体使用和区别。

1.schedule

schedule 方法只能执行一次定时任务,它需要传递 3 个参数:

  • 第 1 个参数:传递一个任务,Runnable 或 Callable 对象;
  • 第 2 个参数:添加定时任务后,再过多久开始执行定时任务;
  • 第 3 个参数:时间单位,配合参数 2 一起使用。

下面我们创建一个 3 秒以后执行的定时任务:

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExample {
    public static void main(String[] args) throws InterruptedException {
        // 创建 ScheduledThreadPool 线程池
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
        System.out.println("schedule 方法添加任务:" + LocalDateTime.now());
        threadPool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行 schedule 方法:" + LocalDateTime.now());
            }
        }, 3, TimeUnit.SECONDS); // 3s 之后执行

        // 以下代码是给业务方法一个时间对照信息
        TimeUnit.SECONDS.sleep(10); // 休眠 10s
        System.out.println("当前时间:" + LocalDateTime.now());
    }
}

以上程序的执行结果如下图所示:

从上述结果中可以看出,使用 schedule 方法只能执行一次定时任务。

2.scheduleAtFixedRate

scheduleAtFixedRate 方法可以执行多次定时任务,此方法需要 4 个参数:

  • 第 1 个参数:传递一个任务,Runnable 或 Callable 对象;
  • 第 2 个参数:添加定时任务后,再过多久开始执行定时任务;
  • 第 3 个参数:定时任务执行的时间间隔;
  • 第 4 个参数:时间单位,配合参数 2 和参数 3 一起使用。

下面我们创建一个 3 秒后执行的定时任务,每个定时任务执行的时间间隔为 2 秒,实现代码如下:

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExample {
    public static void main(String[] args) throws InterruptedException {
        // 创建 ScheduledThreadPool 线程池
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
        System.out.println("scheduleAtFixedRate 方法添加任务:" + LocalDateTime.now());
        threadPool.scheduleAtFixedRate(new Runnable() {
                                           @Override
                                           public void run() {
                                               System.out.println("执行 scheduleAtFixedRate 方法:" + LocalDateTime.now());
                                               // 休眠 2s
                                               try {
                                                   TimeUnit.SECONDS.sleep(2);
                                               } catch (InterruptedException e) {
                                                   e.printStackTrace();
                                               }
                                           }
                                       },
                3L, // 3s 后开始执行定时任务
                2L, // 定时任务的执行间隔为 2s
                TimeUnit.SECONDS); // 描述上面两个参数的时间单位
    }
}

以上程序的执行结果如下图所示:

从上述结果可以看出,当任务添加成功之后,3s 后开始执行第一个定时任务,之后每隔 2s 执行一次定时任务。

3.scheduleWithFixedDelay

scheduleWithFixedDelay 方法的使用和 scheduleAtFixedRate 类似,但执行效果完全不同,这个很容易理解如果效果一样就不用创建两个方法了。

scheduleWithFixedDelay 方法是在方法执行完成之后,再隔 N 秒执行下一个定时任务,和 scheduleAtFixedRate 的固定时间执行不同,scheduleWithFixedDelay 方法的执行受定时任务执行的时长影响,比如以下代码:

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExample {
    public static void main(String[] args) throws InterruptedException {
        // 创建 ScheduledThreadPool 线程池
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
        System.out.println("scheduleWithFixedDelay 方法添加任务:" + LocalDateTime.now());
        threadPool.scheduleWithFixedDelay(new Runnable() {
                                              @Override
                                              public void run() {
                                                  System.out.println("执行 scheduleWithFixedDelay 方法:" + LocalDateTime.now());
                                                  // 休眠 2s
                                                  try {
                                                      TimeUnit.SECONDS.sleep(2);
                                                  } catch (InterruptedException e) {
                                                      e.printStackTrace();
                                                  }
                                              }
                                          },
                3L, // 3s 后开始执行定时任务
                2L, // 定时任务执行完 2s 之后,再执行下一个定时任务
                TimeUnit.SECONDS); // 描述上面两个参数的时间单位
    }
}

以上程序的执行结果如下图所示:

从上述结果可以看出,定时任务在 3s 之后开始执行,以后每隔 4s 执行一次,这 4s 包含了,定时任务执行花费的 2s,加上每隔 2s 执行一次的时间间隔,也就是说 scheduleWithFixedDelay 是在任务执行完 N 秒之后,再执行下一次定时任务

总结

线程池执行定时任务的实现方法有 3 个:

  1. 使用 schedule 方法执行定时任务,只执行一次定时任务。
  2. 使用 scheduleAtFixedRate 方法执行定时任务,执行多次定时任务,它的执行时间间隔是固定的,不受定时任务执行时长影响(定时任务时间间隔 > 任务执行时间)。
  3. 使用 scheduleWithFixedDelay 方法执行定时任务,执行多次定时任务,它是在定时任务执行完之后,再隔 N 秒开始执行下一次定时任务,它的执行时间受定时任务执行时长影响。


是非审之于己,毁誉听之于人,得失安之于数。

相关文章

Java项目中的定时任务调度:掌控时间的艺术

Java项目中的定时任务调度:掌控时间的艺术在Java项目的世界里,时间管理是一个至关重要的技能。就像一个熟练的乐队指挥家需要掌握每一件乐器的演奏时机一样,程序员也需要精通如何安排代码的执行时刻。今天...

Java---定时任务的实现方式

一 什么是定时任务见名知意,定时任务就是每隔一段时间执行一次这个任务,比如我们日常生活中的下课铃,或者是闹钟等等,就是在设置好的固定时间段去不断执行这个任务。二 如何实现定时任务功能这次我介绍两种执行...

再见 Spring Task,这个定时任务框架真香

最近有朋友问到定时任务相关的问题。于是,我简单写了一篇文章总结一下定时任务的一些概念以及一些常见的定时任务技术选型。希望能对小伙伴们有帮助!个人能力有限。如果文章有任何需要补充/完善/修改的地方,欢迎...

java的定时任务解决方案有哪些?你会几种?

一、 业务中的定时任务,java语言有哪些解决方案产品经理说要定时发邮件,定时修改积分,定时发送短信。在我们的开发过程中,经常需要用到定时任务。像php,python,sh,这些脚本语言,一般是配合l...

JAVA架构师之路-教你如何去实现一个分布式定时任务

什么是分布式定时任务:首先,我们要了解计划任务这个概念,计划任务是指由计划的定时运行或者周期性运行的程序。我们最常见的就是Linux的‘crontab’和Windows的‘计划任务’。那么什么是分布式...

Spring Schedule定时用法详解

java开发的小伙伴们在业务开发中需要用到执行定时任务的情况下非常多,今天就介绍下使用Spring Schedule来做定时任务。简介Spring Schedule是一个强大的工具,用于在Java应用...