java.lang.Objectjava.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ScheduledThreadPoolExecutor
public class ScheduledThreadPoolExecutor
ThreadPoolExecutor,它可另行安排在给定的延迟后运行命令,或者定期执行命令。需要多个辅助线程时,或者要求 ThreadPoolExecutor 具有额外的灵活性或功能时,此类要优于 Timer。
一旦启用已延迟的任务就执行它,但是有关何时启用,启用后何时执行则没有任何实时保证。按照提交的先进先出 (FIFO) 顺序来启用那些被安排在同一执行时间的任务。
虽然此类继承自 ThreadPoolExecutor,但是几个继承的调整方法对此类并无作用。特别是,因为它作为一个使用 corePoolSize 线程和一个无界队列的固定大小的池,所以调整 maximumPoolSize 没有什么效果。
扩展注意事项:此类重写 AbstractExecutorService 的 submit 方法,以生成内部对象控制每个任务的延迟和调度。若要保留功能性,子类中任何进一步重写的这些方法都必须调用超类版本,超类版本有效地禁用附加任务的定制。但是,此类提供替代受保护的扩展方法 decorateTask(为 Runnable 和 Callable 各提供一种版本),可定制用于通过 execute、submit、schedule、scheduleAtFixedRate 和 scheduleWithFixedDelay 进入的执行命令的具体任务类型。默认情况下,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.
}
| 嵌套类摘要 |
|---|
| 从类 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
|
decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task) 修改或替换用于执行 callable 的任务。 |
|
protected
|
decorateTask(Runnable runnable, RunnableScheduledFuture<V> task) 修改或替换用于执行 runnable 的任务。 |
|
void |
execute(Runnable command) 使用所要求的零延迟执行命令。 |
|
boolean |
getContinueExistingPeriodicTasksAfterShutdownPolicy() 获取有关在此执行程序已 shutdown 的情况下、是否继续执行现有定期任务的策略。 |
|
boolean |
getExecuteExistingDelayedTasksAfterShutdownPolicy() 获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。 |
|
BlockingQueue<Runnable> |
getQueue() 返回此执行程序使用的任务队列。 |
|
boolean |
remove(Runnable task) 从执行程序的内部队列中移除此任务(如果存在),从而如果尚未开始,则其不再运行。 |
|
|
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() 尝试停止所有正在执行的任务、暂停等待任务的处理,并返回等待执行的任务列表。 |
|
|
submit(Callable<T> task) 提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。 |
|
Future<?> |
submit(Runnable task) 提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。 |
|
|
submit(Runnable task, T result) 提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。 |
|
| 从类 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 |
| 构造方法详细信息 |
|---|
public ScheduledThreadPoolExecutor(int corePoolSize)
corePoolSize - 池中所保存的线程数(包括空闲线程)
IllegalArgumentException - 如果
corePoolSize < 0
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory)
corePoolSize - 池中所保存的线程数(包括空闲线程)
threadFactory - 执行程序创建新线程时使用的工厂
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果 threadFactory 为 null
public ScheduledThreadPoolExecutor(int corePoolSize,
RejectedExecutionHandler handler)
corePoolSize - 池中所保存的线程数(包括空闲线程)
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果处理程序为 null
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
corePoolSize - 池中所保存的线程数(包括空闲线程)
threadFactory - 执行程序创建新线程时使用的工厂
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果 threadFactory 或处理程序为 null
| 方法详细信息 |
|---|
public boolean remove(Runnable task)
ThreadPoolExecutor 复制的描述
此方法可用作取消方案的一部分。它可能无法移除在放置到内部队列之前已经转换为其他形式的任务。例如,使用 submit 输入的任务可能被转换为维护 Future 状态的形式。但是,在此情况下,ThreadPoolExecutor.purge() 方法可用于移除那些已被取消的 Future。
ThreadPoolExecutor 中的
remove
task - 要移除的任务
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
runnable - 所提交的 Runnable
task - 执行 runnable 所创建的任务
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
callable - 所提交的 Callable
task - 执行 callable 所创建的任务
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
ScheduledExecutorService 复制的描述
ScheduledExecutorService 中的
schedule
command - 要执行的任务
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
ScheduledExecutorService 复制的描述
ScheduledExecutorService 中的
schedule
callable - 要执行的功能
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
ScheduledExecutorService 复制的描述
ScheduledExecutorService 中的
scheduleAtFixedRate
command - 要执行的任务
initialDelay - 首次执行的延迟时间
period - 连续执行之间的周期
unit - initialDelay 和 period 参数的时间单位
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
ScheduledExecutorService 复制的描述
ScheduledExecutorService 中的
scheduleWithFixedDelay
command - 要执行的任务
initialDelay - 首次执行的延迟时间
delay - 一次执行终止和下一次执行开始之间的延迟
unit - initialDelay 和 delay 参数的时间单位
public void execute(Runnable command)
ScheduledFuture,而不是
command 本身。
Executor 中的
execute
ThreadPoolExecutor 中的
execute
command - 要执行的任务。
由 -
RejectedExecutionHandler 随意决定的 RejectedExecutionException,如果由于执行程序已关闭而无法接受要执行的任务 。
NullPointerException - 如果 command 为 null。
public Future<?> submit(Runnable task)
ExecutorService 复制的描述
ExecutorService 中的
submit
AbstractExecutorService 中的
submit
task - 要提交的任务
public <T> Future<T> submit(Runnable task, T result)
ExecutorService 复制的描述
ExecutorService 中的
submit
AbstractExecutorService 中的
submit
task - 要提交的任务
result - 返回的结果
public <T> Future<T> submit(Callable<T> task)
ExecutorService 复制的描述
如果想立即阻塞任务的等待,则可以使用 result = exec.submit(aCallable).get(); 形式的构造。
注:Executors 类包括了一组方法,可以转换某些其他常见的类似于闭包的对象,例如,将 PrivilegedAction 转换为 Callable 形式,这样就可以提交它们了。
ExecutorService 中的
submit
AbstractExecutorService 中的
submit
task - 要提交的任务
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
value - 如果为 true,则在关闭后继续执行;否则不执行。
getContinueExistingPeriodicTasksAfterShutdownPolicy()
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
value - 如果为 true,则在关闭后执行;否则不执行。
getExecuteExistingDelayedTasksAfterShutdownPolicy()
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)
public void shutdown()
ExecutorService 中的
shutdown
ThreadPoolExecutor 中的
shutdown
public List<Runnable> shutdownNow()
虽然尽最大努力,但并不保证可以停止处理正在执行的任务。此实现通过 Thread.interrupt() 取消任务,所以任何无法响应中断的任务都可能永远无法终止。
ExecutorService 中的
shutdownNow
ThreadPoolExecutor