模块 java.base

类 ScheduledThreadPoolExecutor

所有已实现的接口:
AutoCloseable , Executor , ExecutorService , ScheduledExecutorService

public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
ThreadPoolExecutor 可以额外安排命令在给定延迟后运行,或定期执行。当需要多个工作线程时,或者当需要 ThreadPoolExecutor (此类扩展)的额外灵活性或功能时,此类优于 Timer

延迟任务在启用后立即执行,但无法实时保证启用后何时开始。安排在完全相同执行时间的任务以先进先出 (FIFO) 的提交顺序启用。

当提交的任务在运行前被取消时,执行将被抑制。默认情况下,此类已取消的任务不会自动从工作队列中删除,直到其延迟结束。虽然这可以进行进一步的检查和监控,但它也可能导致无限保留已取消的任务。为避免这种情况,请使用 setRemoveOnCancelPolicy(boolean) 使任务在取消时立即从工作队列中删除。

通过 scheduleAtFixedRate scheduleWithFixedDelay 安排的周期性任务的连续执行不会重叠。虽然不同的执行可能由不同的线程执行,但先前执行的效果发生在之前后续执行的效果。

虽然此类继承自 ThreadPoolExecutor ,但一些继承的调整方法对它没有用处。特别是,因为它充当使用 corePoolSize 线程和无界队列的固定大小的池,所以对 maximumPoolSize 的调整没有任何有用的效果。此外,将 corePoolSize 设置为零或使用 allowCoreThreadTimeOut 几乎不是一个好主意,因为这可能会使池中没有线程来处理任务,一旦它们有资格运行。

ThreadPoolExecutor 一样,如果没有另外指定,此类使用 Executors.defaultThreadFactory() 作为默认线程工厂,使用 ThreadPoolExecutor.AbortPolicy 作为默认拒绝执行处理程序。

扩展说明:此类覆盖 execute submit 方法以生成内部 ScheduledFuture 对象来控制每个任务的延迟和调度。为了保留功能,子类中这些方法的任何进一步覆盖都必须调用超类版本,这有效地禁用了额外的任务定制。但是,此类提供替代的受保护扩展方法 decorateTaskRunnableCallable 各一个版本),可用于自定义具体任务类型,这些任务类型用于执行通过 executesubmitschedulescheduleAtFixedRatescheduleWithFixedDelay 输入的命令。默认情况下,ScheduledThreadPoolExecutor 使用扩展 FutureTask 的任务类型。但是,这可以使用以下形式的子类进行修改或替换:

 
 public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {

  static class CustomTask<V> implements RunnableScheduledFuture<V> { ... }

  protected <V> RunnableScheduledFuture<V> decorateTask(
        Runnable r, RunnableScheduledFuture<V> task) {
    return new CustomTask<V>(r, task);
  }

  protected <V> RunnableScheduledFuture<V> decorateTask(
        Callable<V> c, RunnableScheduledFuture<V> task) {
    return new CustomTask<V>(c, task);
  }
  // ... add constructors, etc.
 } 
自从:
1.5
  • 构造方法详细信息

    • ScheduledThreadPoolExecutor

      public ScheduledThreadPoolExecutor(int corePoolSize)
      使用给定的核心池大小创建一个新的 ScheduledThreadPoolExecutor
      参数:
      corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了 allowCoreThreadTimeOut
      抛出:
      IllegalArgumentException - 如果 corePoolSize < 0
    • ScheduledThreadPoolExecutor

      public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory  threadFactory)
      使用给定的初始参数创建一个新的 ScheduledThreadPoolExecutor
      参数:
      corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了 allowCoreThreadTimeOut
      threadFactory - 执行程序创建新线程时使用的工厂
      抛出:
      IllegalArgumentException - 如果 corePoolSize < 0
      NullPointerException - 如果 threadFactory 为空
    • ScheduledThreadPoolExecutor

      public ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler  handler)
      使用给定的初始参数创建一个新的 ScheduledThreadPoolExecutor
      参数:
      corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了 allowCoreThreadTimeOut
      handler - 由于达到线程边界和队列容量而阻塞执行时使用的处理程序
      抛出:
      IllegalArgumentException - 如果 corePoolSize < 0
      NullPointerException - 如果 handler 为空
    • ScheduledThreadPoolExecutor

      public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory  threadFactory, RejectedExecutionHandler  handler)
      使用给定的初始参数创建一个新的 ScheduledThreadPoolExecutor
      参数:
      corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了 allowCoreThreadTimeOut
      threadFactory - 执行程序创建新线程时使用的工厂
      handler - 由于达到线程边界和队列容量而阻塞执行时使用的处理程序
      抛出:
      IllegalArgumentException - 如果 corePoolSize < 0
      NullPointerException - 如果 threadFactoryhandler 为空
  • 方法详情

    • decorateTask

      protected <V> RunnableScheduledFuture <V> decorateTask(Runnable  runnable, RunnableScheduledFuture <V> task)
      修改或替换用于执行可运行对象的任务。此方法可用于重写用于管理内部任务的具体类。默认实现只是返回给定的任务。
      类型参数:
      V - 任务结果的类型
      参数:
      runnable - 提交的 Runnable
      task - 为执行可运行对象而创建的任务
      返回:
      可以执行runnable的任务
      自从:
      1.6
    • decorateTask

      protected <V> RunnableScheduledFuture <V> decorateTask(Callable <V> callable, RunnableScheduledFuture <V> task)
      修改或替换用于执行可调用的任务。此方法可用于重写用于管理内部任务的具体类。默认实现只是返回给定的任务。
      类型参数:
      V - 任务结果的类型
      参数:
      callable - 提交的 Callable
      task - 为执行可调用而创建的任务
      返回:
      可以执行可调用的任务
      自从:
      1.6
    • schedule

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

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

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

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

      随后的执行被抑制。在返回的 future 上对 isDone() 的后续调用将返回 true

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

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

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

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

      随后的执行被抑制。在返回的 future 上对 isDone() 的后续调用将返回 true
      指定者:
      scheduleWithFixedDelay 在接口 ScheduledExecutorService
      参数:
      command - 要执行的任务
      initialDelay - 延迟第一次执行的时间
      delay - 一次执行终止与下一次执行开始之间的延迟
      unit——initialDelay和delay参数的时间单位
      返回:
      一个 ScheduledFuture 表示一系列重复任务的待完成。 future的get() 方法永远不会正常返回,会在任务取消或任务执行异常终止时抛出异常。
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果命令或单位为空
      IllegalArgumentException - 如果延迟小于或等于零
    • execute

      public void execute(Runnable  command)
      执行 command 所需的延迟为零。这相当于 schedule(command, 0, anyUnit) 的效果。请注意,对 shutdownNow 返回的队列和列表的检查将访问零延迟 ScheduledFuture ,而不是 command 本身。

      使用 ScheduledFuture 对象的结果是 afterExecute 总是使用空的第二个 Throwable 参数调用,即使 command 突然终止。相反,此类任务抛出的 Throwable 可以通过 Future.get() 获得。

      指定者:
      execute 在接口 Executor
      重写:
      execute 在类 ThreadPoolExecutor
      参数:
      command - 要执行的任务
      抛出:
      RejectedExecutionException - 由 RejectedExecutionHandler 决定,如果由于执行程序已关闭而无法接受任务执行
      NullPointerException - 如果 command 为空
    • submit

      public Future <?> submit(Runnable  task)
      从接口 ExecutorService 复制的描述
      提交一个 Runnable 任务以供执行并返回一个代表该任务的 Future。 Future 的 get 方法将在 successful 完成后返回 null
      指定者:
      submit 在接口 ExecutorService
      重写:
      submit 在类 AbstractExecutorService
      参数:
      task - 要提交的任务
      返回:
      代表任务未决完成的 Future
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果任务为空
    • submit

      public <T> Future <T> submit(Runnable  task, T result)
      从接口 ExecutorService 复制的描述
      提交一个 Runnable 任务以供执行并返回一个代表该任务的 Future。 Future 的 get 方法将在成功完成后返回给定的结果。
      指定者:
      submit 在接口 ExecutorService
      重写:
      submit 在类 AbstractExecutorService
      类型参数:
      T - 结果的类型
      参数:
      task - 要提交的任务
      result - 要返回的结果
      返回:
      代表任务未决完成的 Future
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果任务为空
    • submit

      public <T> Future <T> submit(Callable <T> task)
      从接口 ExecutorService 复制的描述
      提交一个有返回值的任务以供执行,并返回一个代表任务未决结果的 Future。 Future 的 get 方法将在成功完成后返回任务结果。

      如果你想立即阻塞等待任务,你可以使用 result = exec.submit(aCallable).get(); 形式的结构

      注意:Executors 类包含一组方法,可以将一些其他常见的类似闭包的对象(例如,PrivilegedAction 转换为 Callable 形式)以便提交。

      指定者:
      submit 在接口 ExecutorService
      重写:
      submit 在类 AbstractExecutorService
      类型参数:
      T - 任务结果的类型
      参数:
      task - 要提交的任务
      返回:
      代表任务未决完成的 Future
      抛出:
      RejectedExecutionException - 如果无法安排任务执行
      NullPointerException - 如果任务为空
    • setContinueExistingPeriodicTasksAfterShutdownPolicy

      public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
      设置是否继续执行现有周期性任务的策略,即使此执行程序已 shutdown 。在这种情况下,执行将继续,直到 shutdownNow 或策略设置为 false 时已经关闭。该值默认为 false
      参数:
      value - 如果是 true ,关机后继续,否则不要
      参见:
    • getContinueExistingPeriodicTasksAfterShutdownPolicy

      public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
      获取有关是否继续执行现有周期性任务的策略,即使此执行程序已 shutdown 。在这种情况下,执行将继续,直到 shutdownNow 或策略设置为 false 时已经关闭。该值默认为 false
      返回:
      true if 会在关机后继续
      参见:
    • setExecuteExistingDelayedTasksAfterShutdownPolicy

      public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
      设置是否执行现有延迟任务的策略,即使此执行程序已 shutdown 。在这种情况下,这些任务只会在 shutdownNow 时终止,或者在已经关闭时将策略设置为 false 后终止。该值默认为 true
      参数:
      value - 如果是 true ,则在关机后执行,否则不执行
      参见:
    • getExecuteExistingDelayedTasksAfterShutdownPolicy

      public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
      获取有关是否执行现有延迟任务的策略,即使此执行程序已成为 shutdown 。在这种情况下,这些任务只会在 shutdownNow 时终止,或者在已经关闭时将策略设置为 false 后终止。该值默认为 true
      返回:
      true if 会在关机后执行
      参见:
    • setRemoveOnCancelPolicy

      public void setRemoveOnCancelPolicy(boolean value)
      设置取消任务是否应在取消时立即从工作队列中删除的策略。该值默认为 false
      参数:
      value - 如果是 true ,取消时删除,否则不要
      自从:
      1.7
      参见:
    • getRemoveOnCancelPolicy

      public boolean getRemoveOnCancelPolicy()
      获取有关取消任务是否应在取消时立即从工作队列中删除的策略。该值默认为 false
      返回:
      true 如果取消的任务立即从队列中删除
      自从:
      1.7
      参见:
    • shutdown

      public void shutdown()
      启动有序关闭,其中执行先前提交的任务,但不会接受新任务。如果已经关闭,则调用没有额外效果。

      该方法不等待之前提交的任务完成执行。使用 awaitTermination 来做到这一点。

      如果 ExecuteExistingDelayedTasksAfterShutdownPolicy 已设置 false ,则取消延迟尚未结束的现有延迟任务。除非 ContinueExistingPeriodicTasksAfterShutdownPolicy 已设置 true ,否则现有周期性任务的未来执行将被取消。

      指定者:
      shutdown 在接口 ExecutorService
      重写:
      shutdown 在类 ThreadPoolExecutor
      抛出:
      SecurityException - 如果存在安全管理器并且关闭此 ExecutorService 可能会操作不允许调用者修改的线程,因为它不包含 RuntimePermission ("modifyThread") ,或者安全管理器的 checkAccess 方法拒绝访问。
    • shutdownNow

      public List <Runnable > shutdownNow()
      尝试停止所有正在执行的任务,停止等待任务的处理,并返回等待执行的任务列表。从此方法返回后,这些任务将从任务队列中排出(删除)。

      此方法不等待主动执行的任务终止。使用 awaitTermination 来做到这一点。

      除了尽最大努力停止处理正在执行的任务之外,没有任何保证。此实现通过 Thread.interrupt() 中断任务;任何未能响应中断的任务可能永远不会终止。

      指定者:
      shutdownNow 在接口 ExecutorService
      重写:
      shutdownNow 在类 ThreadPoolExecutor
      返回:
      从未开始执行的任务列表。此list的每个元素都是一个 ScheduledFuture 。对于通过 schedule 方法之一提交的任务,该元素将与返回的 ScheduledFuture 相同。对于使用 execute 提交的任务,元素将是零延迟 ScheduledFuture
      抛出:
      SecurityException - 如果存在安全管理器并且关闭此 ExecutorService 可能会操作不允许调用者修改的线程,因为它不包含 RuntimePermission ("modifyThread") ,或者安全管理器的 checkAccess 方法拒绝访问。
    • getQueue

      public BlockingQueue <Runnable > getQueue()
      返回此执行程序使用的任务队列。访问任务队列主要用于调试和监控。该队列可能正在使用中。检索任务队列不会阻止排队任务的执行。

      该队列的每个元素都是一个 ScheduledFuture 。对于通过 schedule 方法之一提交的任务,该元素将与返回的 ScheduledFuture 相同。对于使用 execute 提交的任务,元素将是零延迟 ScheduledFuture

      迭代这个队列是not保证按照任务执行的顺序遍历任务。

      重写:
      getQueue 在类 ThreadPoolExecutor
      返回:
      任务队列