Interview Assault 34: How to use thread pool to perform timed tasks?

In the Java language, there are two thread pools that can perform timed tasks: ScheduledThreadPool and SingleThreadScheduledExecutor, of which SingleThreadScheduledExecutor can be regarded as a single-threaded version of ScheduledThreadPool, and its usage is the same as ScheduledThreadPool, so this article focuses on the use of ScheduledThreadPool thread pools . There are three methods for ScheduledThreadPool to perform timed tasks:

  1. Use the schedule method to execute the scheduled task, and only execute the scheduled task once.
  2. Use the scheduleAtFixedRate method to perform timed tasks and perform multiple timed tasks.
  3. Use the scheduleWithFixedDelay method to perform timed tasks and perform multiple timed tasks.

Next, we look at the specific use and differences of these three methods.

1.schedule

The schedule method can only execute a scheduled task once, and it needs to pass 3 parameters:

  • The first parameter: pass a task, Runnable or Callable object;
  • The second parameter: after adding a scheduled task, how long does it take to start the scheduled task;
  • The third parameter: time unit, used together with parameter 2.

Let's create a scheduled task that will be executed after 3 seconds:

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());
    }
}
复制代码

The execution result of the above program is shown in the following figure: image.pngIt can be seen from the above result that the scheduled task can only be executed once using the schedule method.

2.scheduleAtFixedRate

The scheduleAtFixedRate method can perform multiple scheduled tasks. This method requires 4 parameters:

  • The first parameter: pass a task, Runnable or Callable object;
  • The second parameter: after adding a scheduled task, how long does it take to start the scheduled task;
  • The third parameter: the time interval for the execution of the scheduled task;
  • The fourth parameter: time unit, used together with parameter 2 and parameter 3.

Next, we create a timed task that is executed after 3 seconds. The time interval between each timed task execution is 2 seconds. The implementation code is as follows:

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); // 描述上面两个参数的时间单位
    }
}
复制代码

以上程序的执行结果如下图所示: image.png 从上述结果可以看出,当任务添加成功之后,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); // 描述上面两个参数的时间单位
    }
}
复制代码

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

总结

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

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

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

公众号:Java面试真题解析

面试合集:gitee.com/mydb/interv…

Guess you like

Origin juejin.im/post/7079976022199762957