public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
ThreadPoolExecutor可另外调度在给定延迟之后运行的命令,或定期执行。
该类优选的是Timer需要多个工作线程时,或当附加灵活性或能力ThreadPoolExecutor需要(这此类扩展)。
延迟任务执行时间早于启用,但没有任何实时保证,在启用后,他们将开始。 计划执行完全相同执行时间的任务将以先进先出(FIFO)的提交顺序启用。
提交的任务在运行之前被取消,执行被抑制。 默认情况下,这样一个取消的任务在工作队列中不会自动删除,直到其延迟过去。 虽然这样可以进一步检查和监控,但也可能导致取消任务的无限制保留。 为避免这种情况,请将setRemoveOnCancelPolicy(boolean)设置为true ,这将导致任务在取消时立即从工作队列中删除。
通过scheduleAtFixedRate或scheduleWithFixedDelay的任务的scheduleWithFixedDelay执行不重叠。 虽然不同的执行可以通过不同的线程来执行,先前执行的效果happen-before那些随后的那些的。
虽然这个类继承自ThreadPoolExecutor ,但是一些继承的调优方法对它没有用。 特别是因为它作为使用corePoolSize线程和无限队列的固定大小的池,对maximumPoolSize没有任何有用的效果。 此外,将corePoolSize设置为零或使用allowCoreThreadTimeOut几乎不是一个好主意,因为这可能会使池没有线程来处理任务,只要它们有资格运行。
扩展笔记:此类覆盖execute和submit方法以生成内部ScheduledFuture对象来控制每个任务的延迟和调度。 为了保护功能,子类中这些方法的任何进一步覆盖都必须调用超类版本,这有效地禁用其他任务自定义。 然而,此类提供替代保护扩展方法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. }
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy| Constructor and Description |
|---|
ScheduledThreadPoolExecutor(int corePoolSize)
创建一个新的
ScheduledThreadPoolExecutor与给定的核心池大小。
|
ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
使用给定的初始参数创建一个新的ScheduledThreadPoolExecutor。
|
ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
创建一个新的
ScheduledThreadPoolExecutor与给定的初始参数。
|
ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
使用给定的初始参数创建一个新的ScheduledThreadPoolExecutor。
|
| Modifier and Type | Method and Description |
|---|---|
protected <V> RunnableScheduledFuture<V> |
decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
修改或替换用于执行可调用的任务。
|
protected <V> RunnableScheduledFuture<V> |
decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
修改或替换用于执行runnable的任务。
|
void |
execute(Runnable command)
执行
command零要求延迟。
|
boolean |
getContinueExistingPeriodicTasksAfterShutdownPolicy()
获得关于是否继续执行现有定期任务的策略,即使该执行者已经是
shutdown 。
|
boolean |
getExecuteExistingDelayedTasksAfterShutdownPolicy()
获得有关是否执行现有延迟任务的政策,即使这个执行者已经是
shutdown 。
|
BlockingQueue<Runnable> |
getQueue()
返回此执行程序使用的任务队列。
|
boolean |
getRemoveOnCancelPolicy()
获取关于在取消时是否应立即将已取消任务从工作队列中删除的策略。
|
<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 |
setRemoveOnCancelPolicy(boolean value)
设置取消时取消任务是否应立即从工作队列中删除的策略。
|
void |
shutdown()
启动有序关闭,其中先前提交的任务将被执行,但不会接受任何新任务。
|
List<Runnable> |
shutdownNow()
尝试停止所有主动执行的任务,停止等待任务的处理,并返回正在等待执行的任务列表。
|
<T> Future<T> |
submit(Callable<T> task)
提交值返回任务以执行,并返回代表任务待处理结果的Future。
|
Future<?> |
submit(Runnable task)
提交一个可运行的任务执行,并返回一个表示该任务的未来。
|
<T> Future<T> |
submit(Runnable task, T result)
提交一个可运行的任务执行,并返回一个表示该任务的未来。
|
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated, toStringinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskForclone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitawaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminatedpublic ScheduledThreadPoolExecutor(int corePoolSize)
ScheduledThreadPoolExecutor与给定的核心池大小。
corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了
allowCoreThreadTimeOut
IllegalArgumentException - 如果
corePoolSize < 0
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory)
ScheduledThreadPoolExecutor给定的初始参数。
corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了
allowCoreThreadTimeOut
threadFactory - 当执行者创建新线程时使用的工厂
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果
threadFactory为空
public ScheduledThreadPoolExecutor(int corePoolSize,
RejectedExecutionHandler handler)
corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态,除非设置了
allowCoreThreadTimeOut
handler - 执行被阻止时使用的处理程序,因为达到线程限制和队列容量
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果
handler为空
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
corePoolSize - 保留在池中的线程数,即使它们处于空闲状态,除非设置了
allowCoreThreadTimeOut
threadFactory - 执行程序创建新线程时使用的工厂
handler - 执行被阻止时使用的处理程序,因为达到线程限制和队列容量
IllegalArgumentException - 如果
corePoolSize < 0
NullPointerException - 如果
threadFactory或
handler为空
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
V - 任务的结果类型
runnable - 提交的Runnable
task - 创建以执行runnable的任务
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
V - 任务结果的类型
callable - 提交的Callable
task - 创建用于执行可调用的任务
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
ScheduledExecutorService复制
schedule在界面
ScheduledExecutorService
command - 要执行的任务
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
get()方法将返回
null完成后
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果命令为空
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
ScheduledExecutorService复制
schedule在界面
ScheduledExecutorService
V - 可调用结果的类型
callable - 执行的功能
delay - 从现在开始延迟执行的时间
unit - 延迟参数的时间单位
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果callable为null
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
ScheduledExecutorService复制
initialDelay之后开始,然后initialDelay+period ,然后是initialDelay + 2 * period ,等等。
如果任务的执行遇到异常,则后续的执行被抑制。
否则,任务将仅通过取消或终止执行人终止。
如果任务执行时间比其周期长,则后续执行可能会迟到,但不会同时执行。
scheduleAtFixedRate在界面
ScheduledExecutorService
command - 要执行的任务
initialDelay - 延迟第一次执行的时间
period - 连续执行之间的时期
unit - initialDelay和period参数的时间单位
get()方法将在取消时抛出异常
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果命令为空
IllegalArgumentException - 如果期间小于或等于零
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
ScheduledExecutorService复制
scheduleWithFixedDelay在界面
ScheduledExecutorService
command - 要执行的任务
initialDelay - 延迟第一次执行的时间
delay - 一个执行终止和下一个执行的开始之间的延迟
unit - initialDelay和delay参数的时间单位
get()方法将在取消时抛出异常
RejectedExecutionException - 如果该任务无法安排执行
NullPointerException - 如果命令为空
IllegalArgumentException - 如果延迟小于或等于零
public void execute(Runnable command)
command零要求延迟。
这具有相当于schedule(command, 0, anyUnit)的效果 。
请注意,对shutdownNow返回的队列和列表的shutdownNow将访问零延迟的ScheduledFuture ,而不是command本身。
使用的后果ScheduledFuture对象是afterExecute总是调用空第二Throwable说法,即使command突然终止。 相反,这样一个任务抛出的Throwable可以通过Future.get()获得。
execute在接口
Executor
execute在
ThreadPoolExecutor
command - 要执行的任务
RejectedExecutionException - 由RejectedExecutionHandler
RejectedExecutionHandler ,如果由于执行程序已被关闭,任务不能被接受执行
NullPointerException - 如果
command为空
public Future<?> submit(Runnable task)
ExecutorService复制
get方法将返回null 成功完成时。
submit在界面
ExecutorService
submit在
AbstractExecutorService
task - 提交的任务
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
public <T> Future<T> submit(Runnable task, T result)
ExecutorService复制
get方法将在成功完成后返回给定的结果。
submit在界面
ExecutorService
submit在类别
AbstractExecutorService
T - 结果的类型
task - 提交的任务
result - 结果返回
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
public <T> Future<T> submit(Callable<T> task)
ExecutorService复制
get方法将在成功完成后返回任务的结果。
如果您想立即阻止等待任务,您可以使用result = exec.submit(aCallable).get();格式的result = exec.submit(aCallable).get();
注意: Executors类包含一组方法,可以将一些其他常见的类似闭包的对象(例如PrivilegedAction)转换为Callable形式,以便它们可以提交。
submit在界面
ExecutorService
submit在类别
AbstractExecutorService
T - 任务结果的类型
task - 提交的任务
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
shutdown 。
在这种情况下,这些任务只能在shutdownNow或者在已经关闭后将策略设置为false后终止。
此值默认为false 。
value - 如果
true ,关机后继续,否则不要
getContinueExistingPeriodicTasksAfterShutdownPolicy()
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
shutdown 。
在这种情况下,这些任务只能在shutdownNow或者在已经关闭后将策略设置为false才会终止。
此值默认为false 。
true如果将在关机后继续
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
shutdown 。
在这种情况下,这些任务只能在shutdownNow ,或者在已经关闭后将策略设置为false后。
此值默认为true 。
value - 如果
true ,在关机后执行,否则不要
getExecuteExistingDelayedTasksAfterShutdownPolicy()
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
shutdown 。
在这种情况下,这些任务只能在shutdownNow ,或者在已经关闭后将策略设置为false后才能false 。
此值默认为true 。
true如果在关机后执行
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)
public void setRemoveOnCancelPolicy(boolean value)
false 。
value - 如果
true ,取消取消,否则不要
getRemoveOnCancelPolicy()
public boolean getRemoveOnCancelPolicy()
false 。
true如果取消任务立即从队列中删除
setRemoveOnCancelPolicy(boolean)
public void shutdown()
此方法不等待以前提交的任务完成执行。 使用awaitTermination来做到这一点。
如果ExecuteExistingDelayedTasksAfterShutdownPolicy已设置为false ,则其延迟尚未过去的现有延迟任务将被取消。 除了ContinueExistingPeriodicTasksAfterShutdownPolicy已经设置true ,未来执行的定期任务将被取消。
shutdown在界面
ExecutorService
shutdown在类别
ThreadPoolExecutor
SecurityException - 如果安全管理器存在并关闭此ExecutorService可能会操纵调用者不允许修改的线程,因为它不会保留88453974019588 ("modifyThread")或安全管理器的checkAccess方法拒绝访问。
public List<Runnable> shutdownNow()
此方法不等待主动执行的任务终止。 使用awaitTermination做到这一点。
除了努力尝试停止处理积极执行任务之外,没有任何保证。 此实现通过取消任务Thread.interrupt() ,让未能响应中断任何任务可能永远不会终止。
shutdownNow在界面
ExecutorService
shutdownNow在类别
ThreadPoolExecutor
ScheduledFuture ,包括使用提交这些任务execute ,这对于用作零延迟的基础上调度目的ScheduledFuture 。
SecurityException - 如果安全管理器存在并且关闭此ExecutorService可能会操纵调用者不允许修改的线程,因为它不保留RuntimePermission ("modifyThread")或安全管理器的checkAccess方法拒绝访问。
public BlockingQueue<Runnable> getQueue()
ScheduledFuture ,其中包括使用execute提交的任务,这些任务用于作为零延迟的基础的调度目的ScheduledFuture 。
这个队列的迭代不能按照它们执行的顺序遍历任务。
getQueue在类别
ThreadPoolExecutor
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.