ScheduledThreadPoolExecutor (Java Platform SE 8 ) (original) (raw)

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.

When a submitted task is cancelled before it is run, execution is suppressed. By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks. To avoid this, set setRemoveOnCancelPolicy(boolean) to true, which causes tasks to be immediately removed from the work queue at time of cancellation.

Successive executions of a task scheduled viascheduleAtFixedRate orscheduleWithFixedDelay do not overlap. While different executions may be performed by different threads, the effects of prior executions happen-before those of subsequent ones.

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool usingcorePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run.

Extension notes: This class overrides theexecute andsubmit methods to generate internal ScheduledFuture objects to control per-task delays and scheduling. To preserve functionality, any further overrides of these methods in subclasses must invoke superclass versions, which effectively disables additional task customization. However, this class provides alternative protected extension methoddecorateTask (one version each for Runnable andCallable) that can be used to customize the concrete task types used to execute commands entered via execute,submit, schedule, scheduleAtFixedRate, and scheduleWithFixedDelay. By default, aScheduledThreadPoolExecutor uses a task type extendingFutureTask. However, this may be modified or replaced using subclasses of the form:

` 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. }`