ScheduledThreadPoolExecutor
java.util.concurrent
类 ScheduledThreadPoolExecutor
java.lang.Object
继承者 java.util.concurrent.AbstractExecutorService
继承者 java.util.concurrent.ThreadPoolExecutor
继承者 java.util.concurrent.ScheduledThreadPoolExecutor
所有已实现的接口:
Executor, ExecutorService, ScheduledExecutorService
需要多线程时,或者要求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 implements RunnableScheduledFuture { ...
protected RunnableScheduledFuture decorateTask(
Runnable r, RunnableScheduledFuture task) {
return new CustomTask(r, task);
}
protected RunnableScheduledFuture decorateTask(
Callable c, RunnableScheduledFuture task) {
return new CustomTask(c, task);
}
// ... add constructors, etc.
}}
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
corePoolSize)
Creates a new
ScheduledThreadPoolExecutor with the given core pool size. |
||||||||||
|
corePoolSize, ThreadFactory threadFactory)
Creates a new
ScheduledThreadPoolExecutor with the given initial parameters. |
||||||||||
|
corePoolSize, RejectedExecutionHandler handler)
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
|
||||||||||
|
corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
|
主要函数
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Executes
command with zero required delay. |
||||||||||
|
Gets the policy on whether to continue executing existing periodic tasks even when this executor has been
shutdown .
获取有关在此执行程序已shutdown 的情况下、是否继续执行现有定期任务的策略。
|
||||||||||
|
Gets the policy on whether to execute existing delayed tasks even when this executor has been
shutdown .
获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
|
||||||||||
|
Returns the task queue used by this executor.
|
||||||||||
|
long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
|
||||||||||
|
callable, long delay, TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
|
||||||||||
|
long initialDelay, long period, TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period,
then initialDelay + 2 * period, and so on. |
||||||||||
|
long initialDelay, long delay, TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.
|
||||||||||
|
value)
Sets the policy on whether to continue executing existing periodic tasks even when this executor has been
shutdown .
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。
|
||||||||||
|
value)
Sets the policy on whether to execute existing delayed tasks even when this executor has been
shutdown .
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
|
||||||||||
|
Initiates
|