ScheduledThreadPoolExecutor
java.util.concurrent.ScheduledThreadPoolExecutor
继承者 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  ScheduledThreadPoolExecutorwith the given core pool size. | |||||||||||
| ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) Creates a new  ScheduledThreadPoolExecutorwith 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  commandwith 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 an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. | ||||||||||
| List<Runnable> | shutdownNow() Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. | ||||||||||
| <T> Future<T> | submit(Runnable task, T result) Submits a Runnable task for execution and returns a Future representing that task. | ||||||||||
| <T> Future<T> | submit(Callable<T> task) Submits a value-returning task for execution and returns a Future representing the pending results of the task. | ||||||||||
| Future<?> | submit(Runnable task) Submits a Runnable task for execution and returns a Future representing that task. | ||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| <V> RunnableScheduledFuture<V> | decorateTask(Runnable runnable, RunnableScheduledFuture<V> task) Modifies or replaces the task used to execute a runnable. 重写该函数来,以用个性化的RunnableScheduledFuture来包装runnable任务,task是ScheduledThreadPoolExecutor提供默认的默认的对runnable的RunnableSheduledFuture包装,即ScheduledFutureTask对象。 | ||||||||||
| <V> RunnableScheduledFuture<V> | decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task) Modifies or replaces the task used to execute a callable. 重写该函数来,以用个性化的RunnableScheduledFuture来包装callable任务,task是ScheduledThreadPoolExecutor提供默认的默认的对callable的RunnableSheduledFuture包装,即ScheduledFutureTask对象。 | ||||||||||
更多内容请参考《ThreadPoolExecutor》和《ScheduledExecutorService》
Timer与ScheduledThreadPoolExecutor的区别:
Timer对调度的支持是基于绝对时间的,因此任务对系统时间的改变是敏感的;而ScheduledThreadPoolExecutor支持相对时间。
Timer使用单线程方式来执行所有的TimerTask,如果某个TimerTask很耗时则会影响到其他TimerTask的执行;
而ScheduledThreadPoolExecutor则可以构造一个固定大小的线程池来执行任务。
Timer 不会捕获由TimerTask抛出的未检查异常,故当有异常抛出时,Timer会终止,导致未执行完的TimerTask不再执行,新的 TimerTask也不能被调度;
ScheduledThreadPoolExecutor对这个问题进行了妥善的处理,不会影响其他任务的执行。
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号