现在的位置: 首页 > 综合 > 正文

ScheduledThreadPoolExecutor

2012年10月02日 ⁄ 综合 ⁄ 共 4039字 ⁄ 字号 评论关闭

ScheduledThreadPoolExecutor

java.util.concurrent

类 ScheduledThreadPoolExecutor

java.lang.Object

  继承者 java.util.concurrent.AbstractExecutorService

      继承者 java.util.concurrent.ThreadPoolExecutor

          继承者 java.util.concurrent.ScheduledThreadPoolExecutor


所有已实现的接口:

    Executor, ExecutorService, ScheduledExecutorService 
ThreadPoolExecutor相比,ScheduledThreadPoolExecutor它可另行安排在给定的延迟后运行命令,或者定期执行命令

需要多线程时,或者要求ThreadPoolExecutor具有额外的灵活性或功能时,此类要优于Timer

一旦启用已延迟的任务就执行它,但是有关何时启用,启用后何时执行则没有任何实时保证。

按照提交的先进先出 (FIFO) 顺序来启用那些被安排在同一执行时间的任务。

虽然此类继承自 ThreadPoolExecutor,但是几个继承的调整方法对此类并无作用。

特别是,因为它作为一个使用固定 corePoolSize 线程和一个无界队列的固定大小的池,所以调整 maximumPoolSize 没有什么效果。
注意1:"一旦启用已延迟的任务就执行它,但是有关何时启用,启用后何时执行则没有任何实时保证",即没实时性保证。
注意2:注意"因为它作为一个使用 corePoolSize 线程和一个无界队列的固定大小的池,所以调整 maximumPoolSize 没有什么效果",就是说他只有core线程,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
ScheduledThreadPoolExecutor(int
corePoolSize)
Creates a new ScheduledThreadPoolExecutor with the given core pool size.
ScheduledThreadPoolExecutor(int
corePoolSize, ThreadFactory threadFactory)
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
ScheduledThreadPoolExecutor(int
corePoolSize, RejectedExecutionHandler handler)
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
ScheduledThreadPoolExecutor(int
corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

主要函数

Public Methods
void execute(Runnable command)
Executes command with zero required delay.
boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
Gets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown.
获取有关在此执行程序已shutdown 的情况下、是否继续执行现有定期任务的策略。
boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
Gets the policy on whether to execute existing delayed tasks even when this executor has been shutdown.
获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
BlockingQueue<Runnable> getQueue()
Returns the task queue used by this executor.
ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
<V> ScheduledFuture<V> schedule(Callable<V>
callable, long delay, TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
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.
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
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.
void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean
value)
Sets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown.
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。
void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean
value)
Sets the policy on whether to execute existing delayed tasks even when this executor has been shutdown.
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
void shutdown()
Initiates

抱歉!评论已关闭.