| |||||||||
摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 |
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 没有什么效果。
扩展注意事项:此类重写 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. }
- 从以下版本开始:
- 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 | 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 |
构造方法详细信息 |
---|
ScheduledThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize)
- 使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor。
- 参数:
corePoolSize
- 池中所保存的线程数(包括空闲线程)- 抛出:
IllegalArgumentException
- 如果 corePoolSize < 0
ScheduledThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
- 使用给定的初始参数创建一个新 ScheduledThreadPoolExecutor。
- 参数:
corePoolSize
- 池中所保存的线程数(包括空闲线程)threadFactory
- 执行程序创建新线程时使用的工厂- 抛出:
IllegalArgumentException
- 如果 corePoolSize < 0NullPointerException
- 如果 threadFactory 为 null
ScheduledThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
- 使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。
- 参数:
corePoolSize
- 池中所保存的线程数(包括空闲线程)handler
- 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序- 抛出:
IllegalArgumentException
- 如果 corePoolSize < 0NullPointerException
- 如果处理程序为 null
ScheduledThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
- 使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。
- 参数:
corePoolSize
- 池中所保存的线程数(包括空闲线程)threadFactory
- 执行程序创建新线程时使用的工厂handler
- 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序- 抛出:
IllegalArgumentException
- 如果 corePoolSize < 0NullPointerException
- 如果 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
- 所提交的 Runnabletask
- 执行 runnable 所创建的任务- 返回:
- 可以执行 runnable 的任务
- 从以下版本开始:
- 1.6
decorateTask
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
- 修改或替换用于执行 callable 的任务。此方法可重写用于管理内部任务的具体类。默认实现返回给定任务。
- 参数:
callable
- 所提交的 Callabletask
- 执行 callable 所创建的任务- 返回:
- 可以执行 callable 的任务
- 从以下版本开始:
- 1.6