public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
ThreadPoolExecutor
可以另外安排的命令来运行在一个给定的延迟,或定期执行。这个班是最好的
Timer
当多个线程都需要,或当额外的灵活性和能力
ThreadPoolExecutor
(这类扩展)是必需的。
延迟的任务执行没有比他们更快的启用,但没有任何实时保证的时候,当他们被启用后,他们将开始。计划完全相同的执行时间的任务是使先入先出(FIFO)订单提交。
当一个提交的任务在运行之前被取消时,执行被抑制。默认情况下,这样一个被取消的任务不会自动删除从工作队列的延迟直到逝去。虽然这使进一步的检查和监测,它也可能会导致无限保留的取消任务。为了避免这种情况,设置setRemoveOnCancelPolicy(boolean)
到true
,导致任务要立即取消时间从工作队列中移除。
一个任务计划通过scheduleAtFixedRate
或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)
修改或替换用来执行一个可运行的任务。
|
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<?> |
submit(Runnable task)
提交执行一个Runnable任务并返回一个表示该任务的未来。
|
<T> Future<T> |
submit(Runnable task, T result)
提交执行一个Runnable任务并返回一个表示该任务的未来。
|
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, toString
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
public ScheduledThreadPoolExecutor(int corePoolSize)
ScheduledThreadPoolExecutor
。
corePoolSize
-线程的数量保持在游泳池里,即使他们是懒惰的,除非
allowCoreThreadTimeOut
设置
corePoolSize < 0
IllegalArgumentException
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
ScheduledThreadPoolExecutor
。
corePoolSize
-线程的数量保持在游泳池里,即使他们是懒惰的,除非
allowCoreThreadTimeOut
设置
threadFactory
-工厂用当执行创建新线程
corePoolSize < 0
IllegalArgumentException
NullPointerException
-如果
threadFactory
是空的
public ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
corePoolSize
-保持池中的线程数,即使他们是懒惰的,除非
allowCoreThreadTimeOut
设置
handler
-处理程序时使用的执行受阻因为达到线程边界和队列容量
corePoolSize < 0
IllegalArgumentException
NullPointerException
-如果
handler
是空的
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
corePoolSize
-线程的数量保持在游泳池里,即使他们是懒惰的,除非
allowCoreThreadTimeOut
设置
threadFactory
-工厂用当执行创建新线程
handler
-处理程序时使用的执行受阻因为达到线程边界和队列容量
corePoolSize < 0
IllegalArgumentException
NullPointerException
-如果
threadFactory
或
handler
是空的
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
V
-任务类型结果
runnable
-提交的运行
task
创建执行Runnable任务
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
V
-任务类型结果
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
如果赎回是空的
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和周期参数
get()
方法将抛出一个异常后取消
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果命令是无效的
IllegalArgumentException
-如果时间小于或等于零
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
ScheduledExecutorService
scheduleWithFixedDelay
接口
ScheduledExecutorService
command
-任务执行
initialDelay
-时间延迟第一次执行
delay
-一个执行和下一个开始的结束之间的延迟
unit
的initialdelay和延迟参数的时间单位
get()
方法将抛出一个异常后取消
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果命令是无效的
IllegalArgumentException
如果延迟小于或等于零
public void execute(Runnable command)
command
要求执行零延迟。这种效应相当于
schedule(command, 0, anyUnit)
。注意,排队检查和返回的列表
shutdownNow
将接入零延迟
ScheduledFuture
,不是
command
本身。
对ScheduledFuture
对象的使用后果是afterExecute
总是叫空二Throwable
争论,即使command
突然终止。相反,通过这样一个任务把Throwable
可通过Future.get()
。
execute
接口
Executor
execute
方法重写,继承类
ThreadPoolExecutor
command
-任务执行
RejectedExecutionException
在自由裁量权的
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();
结构
注:本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
时已关机。这个值是默认的
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
-如果存在一个安全管理和关闭这个服务可能操纵线程允许修改因为它不拥有
RuntimePermission
("modifyThread")
不来电,或安全经理的
checkAccess
方法拒绝访问。
public List<Runnable> shutdownNow()
此方法不等待主动执行任务终止。使用awaitTermination
做。
有没有保证超出了最好的努力试图停止处理积极执行任务。这个实现取消任务通过Thread.interrupt()
,所以任何任务的失败中断可能永远不会结束。
shutdownNow
接口
ExecutorService
shutdownNow
方法重写,继承类
ThreadPoolExecutor
ScheduledFuture
,包括那些使用
execute
任务提交,这是调度目的作为一零延迟
ScheduledFuture
基础。
SecurityException
-如果存在一个安全管理和关闭这个服务可能操纵线程允许修改因为它不拥有
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.