JavaTM Platform
Standard Ed. 6

java.util.concurrent
类 ScheduledThreadPoolExecutor

java.lang.Object
  继承者 java.util.concurrent.AbstractExecutorService
      继承者 java.util.concurrent.ThreadPoolExecutor
          继承者 java.util.concurrent.ScheduledThreadPoolExecutor
所有已实现的接口:
Executor, ExecutorService, ScheduledExecutorService

public class ScheduledThreadPoolExecutor
     
extends ThreadPoolExecutor
implements ScheduledExecutorService

ThreadPoolExecutor,它可另行安排在给定的延迟后运行命令,或者定期执行命令。需要多个辅助线程时,或者要求 ThreadPoolExecutor 具有额外的灵活性或功能时,此类要优于 Timer

一旦启用已延迟的任务就执行它,但是有关何时启用,启用后何时执行则没有任何实时保证。按照提交的先进先出 (FIFO) 顺序来启用那些被安排在同一执行时间的任务。

虽然此类继承自 ThreadPoolExecutor,但是几个继承的调整方法对此类并无作用。特别是,因为它作为一个使用 corePoolSize 线程和一个无界队列的固定大小的池,所以调整 maximumPoolSize 没有什么效果。

扩展注意事项:此类重写 AbstractExecutorServicesubmit 方法,以生成内部对象控制每个任务的延迟和调度。若要保留功能性,子类中任何进一步重写的这些方法都必须调用超类版本,超类版本有效地禁用附加任务的定制。但是,此类提供替代受保护的扩展方法 decorateTask(为 RunnableCallable 各提供一种版本),可定制用于通过 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

嵌套类摘要
 
从类 java.util.concurrent.ThreadPoolExecutor 继承的嵌套类/接口
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
 
构造方法摘要
ScheduledThreadPoolExecutor(int corePoolSize)
          使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor。
ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
          使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。
ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
          使用给定的初始参数创建一个新 ScheduledThreadPoolExecutor。
ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
          使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。
 
方法摘要
protected
<V> RunnableScheduledFuture<V>
decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
          修改或替换用于执行 callable 的任务。
protected
<V> RunnableScheduledFuture<V>
decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
          修改或替换用于执行 runnable 的任务。
 void execute(Runnable command)
          使用所要求的零延迟执行命令。
 boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
          获取有关在此执行程序已 shutdown 的情况下、是否继续执行现有定期任务的策略。
 boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
          获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
 BlockingQueue<Runnable> getQueue()
          返回此执行程序使用的任务队列。
 boolean remove(Runnable task)
          从执行程序的内部队列中移除此任务(如果存在),从而如果尚未开始,则其不再运行。
<V> ScheduledFuture<V>
schedule(Callable<V> callable, long delay, TimeUnit unit)
          创建并执行在给定延迟后启用的 ScheduledFuture。
 ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
          创建并执行在给定延迟后启用的一次性操作。
 ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
          创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
 ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
          创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
 void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
          设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。
 void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
          设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
 void shutdown()
          在以前已提交任务的执行中发起一个有序的关闭,但是不接受新任务。
 List<Runnable> shutdownNow()
          尝试停止所有正在执行的任务、暂停等待任务的处理,并返回等待执行的任务列表。
<T> Future<T>
submit(Callable<T> task)
          提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。
 Future<?> submit(Runnable task)
          提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。
<T> Future<T>
submit(Runnable task, T result)
          提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。
 
从类 java.util.concurrent.ThreadPoolExecutor 继承的方法
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated
 
从类 java.util.concurrent.AbstractExecutorService 继承的方法
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
 
从类 java.lang.Object 继承的方法
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
从接口 java.util.concurrent.ExecutorService 继承的方法
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
 

构造方法详细信息

ScheduledThreadPoolExecutor

public ScheduledThreadPoolExecutor(int corePoolSize)
使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor。

参数:
corePoolSize - 池中所保存的线程数(包括空闲线程)
抛出:
IllegalArgumentException - 如果 corePoolSize < 0

ScheduledThreadPoolExecutor

public ScheduledThreadPoolExecutor(int corePoolSize,
                                   ThreadFactory threadFactory)
使用给定的初始参数创建一个新 ScheduledThreadPoolExecutor。

参数:
corePoolSize - 池中所保存的线程数(包括空闲线程)
threadFactory - 执行程序创建新线程时使用的工厂
抛出:
IllegalArgumentException - 如果 corePoolSize < 0
NullPointerException - 如果 threadFactory 为 null

ScheduledThreadPoolExecutor

public ScheduledThreadPoolExecutor(int corePoolSize,
                                   RejectedExecutionHandler handler)
使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。

参数:
corePoolSize - 池中所保存的线程数(包括空闲线程)
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
抛出:
IllegalArgumentException - 如果 corePoolSize < 0
NullPointerException - 如果处理程序为 null

ScheduledThreadPoolExecutor

public ScheduledThreadPoolExecutor(int corePoolSize,
                                   ThreadFactory threadFactory,
                                   RejectedExecutionHandler handler)
使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。

参数:
corePoolSize - 池中所保存的线程数(包括空闲线程)
threadFactory - 执行程序创建新线程时使用的工厂
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
抛出:
IllegalArgumentException - 如果 corePoolSize < 0
NullPointerException - 如果 threadFactory 或处理程序为 null
方法详细信息

remove

public boolean remove(Runnable task)
从类 ThreadPoolExecutor 复制的描述
从执行程序的内部队列中移除此任务(如果存在),从而如果尚未开始,则其不再运行。

此方法可用作取消方案的一部分。它可能无法移除在放置到内部队列之前已经转换为其他形式的任务。例如,使用 submit 输入的任务可能被转换为维护 Future 状态的形式。但是,在此情况下,ThreadPoolExecutor.purge() 方法可用于移除那些已被取消的 Future。

覆盖:
ThreadPoolExecutor 中的 remove
参数:
task - 要移除的任务
返回:
如果已经移除任务,则返回 true

decorateTask

protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable,
                                                      RunnableScheduledFuture<V> task)
修改或替换用于执行 runnable 的任务。此方法可重写用于管理内部任务的具体类。默认实现只返回给定任务。

参数:
runnable - 所提交的 Runnable
task - 执行 runnable 所创建的任务
返回:
可以执行 runnable 的任务
从以下版本开始:
1.6

decorateTask

protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable,
                                                      RunnableScheduledFuture<V> task)
修改或替换用于执行 callable 的任务。此方法可重写用于管理内部任务的具体类。默认实现返回给定任务。

参数:
callable - 所提交的 Callable
task - 执行 callable 所创建的任务
返回:
可以执行 callable 的任务
从以下版本开始:
1.6

schedule

public ScheduledFuture<?> schedule(Runnable command,
                                   long delay,
                                   TimeUnit unit)
从接口 ScheduledExecutorService 复制的描述
创建并执行在给定延迟后启用的一次性操作。

指定者:
接口 ScheduledExecutorService 中的 schedule
参数:
command - 要执行的任务
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
返回:
表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在完成后将返回 null

schedule

public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                       long delay,
                                       TimeUnit unit)
从接口 ScheduledExecutorService 复制的描述
创建并执行在给定延迟后启用的 ScheduledFuture。

指定者:
接口 ScheduledExecutorService 中的 schedule
参数:
callable - 要执行的功能
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
返回:
可用于提取结果或取消的 ScheduledFuture

scheduleAtFixedRate

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit)
从接口 ScheduledExecutorService 复制的描述
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。如果任务的任何一个执行遇到异常,则后续执行都会被取消。否则,只能通过执行程序的取消或终止方法来终止该任务。如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。

指定者:
接口 ScheduledExecutorService 中的 scheduleAtFixedRate
参数:
command - 要执行的任务
initialDelay - 首次执行的延迟时间
period - 连续执行之间的周期
unit - initialDelay 和 period 参数的时间单位
返回:
表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常

scheduleWithFixedDelay

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit)
从接口 ScheduledExecutorService 复制的描述
创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。如果任务的任一执行遇到异常,就会取消后续执行。否则,只能通过执行程序的取消或终止方法来终止该任务。

指定者:
接口 ScheduledExecutorService 中的 scheduleWithFixedDelay
参数:
command - 要执行的任务
initialDelay - 首次执行的延迟时间
delay - 一次执行终止和下一次执行开始之间的延迟
unit - initialDelay 和 delay 参数的时间单位
返回:
表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常

execute

public void execute(Runnable command)
使用所要求的零延迟执行命令。这在效果上等同于调用 schedule(command, 0, anyUnit)。注意,对由 shutdownNow 所返回的队列和列表的检查将访问零延迟的 ScheduledFuture,而不是 command 本身。

指定者:
接口 Executor 中的 execute
覆盖:
ThreadPoolExecutor 中的 execute
参数:
command - 要执行的任务。
抛出:
- RejectedExecutionHandler 随意决定的 RejectedExecutionException,如果由于执行程序已关闭而无法接受要执行的任务 。
NullPointerException - 如果 command 为 null。

submit

public Future<?> submit(Runnable task)
从接口 ExecutorService 复制的描述
提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。该 Future 的 get 方法在 成功 完成时将会返回 null

指定者:
接口 ExecutorService 中的 submit
覆盖:
AbstractExecutorService 中的 submit
参数:
task - 要提交的任务
返回:
表示任务等待完成的 Future

submit

public <T> Future<T> submit(Runnable task,
                            T result)
从接口 ExecutorService 复制的描述
提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。该 Future 的 get 方法在成功完成时将会返回给定的结果。

指定者:
接口 ExecutorService 中的 submit
覆盖:
AbstractExecutorService 中的 submit
参数:
task - 要提交的任务
result - 返回的结果
返回:
表示任务等待完成的 Future

submit

public <T> Future<T> submit(Callable<T> task)
从接口 ExecutorService 复制的描述
提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。该 Future 的 get 方法在成功完成时将会返回该任务的结果。

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

注:Executors 类包括了一组方法,可以转换某些其他常见的类似于闭包的对象,例如,将 PrivilegedAction 转换为 Callable 形式,这样就可以提交它们了。

指定者:
接口 ExecutorService 中的 submit
覆盖:
AbstractExecutorService 中的 submit
参数:
task - 要提交的任务
返回:
表示任务等待完成的 Future

setContinueExistingPeriodicTasksAfterShutdownPolicy

public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。在这种情况下,仅在执行 shutdownNow 时,或者在执行程序已关闭、将策略设置为 false 后才终止这些任务。此值默认为 false。

参数:
value - 如果为 true,则在关闭后继续执行;否则不执行。
另请参见:
getContinueExistingPeriodicTasksAfterShutdownPolicy()

getContinueExistingPeriodicTasksAfterShutdownPolicy

public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
获取有关在此执行程序已 shutdown 的情况下、是否继续执行现有定期任务的策略。在这种情况下,仅在执行 shutdownNow 时,或者在执行程序已关闭时将策略设置为 false 后才终止这些任务。此值默认为 false。

返回:
如果关闭后继续执行,则返回 true。
另请参见:
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)

setExecuteExistingDelayedTasksAfterShutdownPolicy

public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。在这种情况下,仅在执行 shutdownNow 时,或者在执行程序已关闭、将策略设置为 false 后才终止这些任务。此值默认为 true。

参数:
value - 如果为 true,则在关闭后执行;否则不执行。
另请参见:
getExecuteExistingDelayedTasksAfterShutdownPolicy()

getExecuteExistingDelayedTasksAfterShutdownPolicy

public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。在这种情况下,仅在执行 shutdownNow 时,或者在执行程序已关闭时将策略设置为 false 后才终止这些任务。此值默认为 true。

返回:
如果关闭后执行,则返回 true
另请参见:
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)

shutdown

public void shutdown()
在以前已提交任务的执行中发起一个有序的关闭,但是不接受新任务。如果已将 ExecuteExistingDelayedTasksAfterShutdownPolicy 设置为 false,则取消尚未超出其延迟的现有延迟任务。并且除非已将 ContinueExistingPeriodicTasksAfterShutdownPolicy 设置为 true,否则将取消现有定期任务的后续执行。

指定者:
接口 ExecutorService 中的 shutdown
覆盖:
ThreadPoolExecutor 中的 shutdown

shutdownNow

public List<Runnable> shutdownNow()
尝试停止所有正在执行的任务、暂停等待任务的处理,并返回等待执行的任务列表。

虽然尽最大努力,但并不保证可以停止处理正在执行的任务。此实现通过 Thread.interrupt() 取消任务,所以任何无法响应中断的任务都可能永远无法终止。

指定者:
接口 ExecutorService 中的 shutdownNow
覆盖:
ThreadPoolExecutor 中的 shutdownNow
返回:
从未开始执行的任务列表。此列表中的每个元素都是一个 ScheduledFuture,包括用 execute 所提交的那些任务,出于安排的目的,这些任务用作零延迟 ScheduledFuture 的基础。
抛出:
SecurityException - 如果安全管理器存在并且关闭此 ExecutorService 可能操作某些不允许调用者修改的线程(因为它没有 RuntimePermission ("modifyThread")),或者安全管理器的 checkAccess 方法拒绝访问。

getQueue

public BlockingQueue<Runnable> getQueue()
返回此执行程序使用的任务队列。此队列中的每个元素都是一个 ScheduledFuture,包括用 execute 所提交的那些任务,出于安排的目的,这些任务用作零延迟 ScheduledFuture 的基础。 无法 保证对此队列进行迭代的迭代器会以任务执行的顺序遍历各任务。

覆盖:
ThreadPoolExecutor 中的 getQueue
返回:
任务队列。

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策