模块 java.base

接口 ScheduledExecutorService

所有父级接口:
AutoCloseable , Executor , ExecutorService
所有已知的实现类:
ScheduledThreadPoolExecutor

public interface ScheduledExecutorService extends ExecutorService
一个 ExecutorService 可以安排命令在给定延迟后运行,或定期执行。

schedule 方法创建具有各种延迟的任务并返回可用于取消或检查执行的任务对象。 scheduleAtFixedRatescheduleWithFixedDelay 方法创建并执行定期运行的任务,直到被取消。

使用 Executor.execute(Runnable) ExecutorService submit 方法提交的命令被安排为请求延迟为零。 schedule 方法中也允许零延迟和负延迟(但不是句点),并被视为立即执行的请求。

所有 schedule 方法都接受 relative 延迟和周期作为参数,而不是绝对时间或日期。将表示为 Date 的绝对时间转换为所需形式是一件简单的事情。例如,要安排在某个未来 date ,您可以使用: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS) 。但是请注意,由于网络时间同步协议、时钟漂移或其他因素,相对延迟的到期不必与任务启用的当前 Date 一致。

Executors 类为该包中提供的 ScheduledExecutorService 实现提供方便的工厂方法。

使用示例

这是一个类,其方法将 ScheduledExecutorService 设置为每十秒发出一次蜂鸣声,持续一小时:
 
 import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
  private final ScheduledExecutorService scheduler =
   Executors.newScheduledThreadPool(1);

  public void beepForAnHour() {
   Runnable beeper = () -> System.out.println("beep");
   ScheduledFuture<?> beeperHandle =
    scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
   Runnable canceller = () -> beeperHandle.cancel(false);
   scheduler.schedule(canceller, 1, HOURS);
  }
 } 
自从:
1.5
  • 方法详情

    • schedule

      ScheduledFuture <?> schedule(Runnable  command, long delay, TimeUnit  unit)
      提交在给定延迟后启用的一次性任务。
      参数:
      command - 要执行的任务
      delay - 从现在开始延迟执行的时间
      unit - delay参数的时间单位
      返回:
      一个 ScheduledFuture 表示任务的未决完成,其 get() 方法将在完成后返回 null
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果命令或单位为空
    • schedule

      <V> ScheduledFuture <V> schedule(Callable <V> callable, long delay, TimeUnit  unit)
      提交在给定延迟后启用的返回值的一次性任务。
      类型参数:
      V - 可调用结果的类型
      参数:
      callable - 要执行的函数
      delay - 从现在开始延迟执行的时间
      unit - delay参数的时间单位
      返回:
      可用于提取结果或取消的 ScheduledFuture
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果可调用或单元为空
    • scheduleAtFixedRate

      ScheduledFuture <?> scheduleAtFixedRate(Runnable  command, long initialDelay, long period, TimeUnit  unit)
      提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在给定的周期内启用;也就是说,执行将在 initialDelay 之后开始,然后是 initialDelay + period ,然后是 initialDelay + 2 * period ,依此类推。

      任务执行顺序无限期地继续,直到发生以下异常完成之一:

      • 任务是 明确取消 通过返回的未来。
      • 执行者终止,也导致任务取消。
      • 任务的执行抛出异常。在这种情况下,对返回的 future 调用 get 将抛出 ExecutionException ,将异常作为其原因。
      随后的执行被抑制。在返回的 future 上对 isDone() 的后续调用将返回 true

      如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会同时执行。

      参数:
      command - 要执行的任务
      initialDelay - 延迟第一次执行的时间
      period - 连续执行之间的时间间隔
      unit - initialDelay 和 period 参数的时间单位
      返回:
      一个 ScheduledFuture 表示一系列重复任务的待完成。 future的get() 方法永远不会正常返回,会在任务取消或任务执行异常终止时抛出异常。
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果命令或单位为空
      IllegalArgumentException - 如果周期小于或等于零
    • scheduleWithFixedDelay

      ScheduledFuture <?> scheduleWithFixedDelay(Runnable  command, long initialDelay, long delay, TimeUnit  unit)
      提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在一次执行的终止和下一次执行的开始之间有给定的延迟。

      任务执行顺序无限期地继续,直到发生以下异常完成之一:

      • 任务是 明确取消 通过返回的未来。
      • 执行者终止,也导致任务取消。
      • 任务的执行抛出异常。在这种情况下,对返回的 future 调用 get 将抛出 ExecutionException ,将异常作为其原因。
      随后的执行被抑制。在返回的 future 上对 isDone() 的后续调用将返回 true
      参数:
      command - 要执行的任务
      initialDelay - 延迟第一次执行的时间
      delay - 一次执行终止与下一次执行开始之间的延迟
      unit——initialDelay和delay参数的时间单位
      返回:
      一个 ScheduledFuture 表示一系列重复任务的待完成。 future的get() 方法永远不会正常返回,会在任务取消或任务执行异常终止时抛出异常。
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果命令或单位为空
      IllegalArgumentException - 如果延迟小于或等于零