public class PausableScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements PausableExecutorService
ScheduledThreadPoolExecutor
that implements PausableExecutorService
.
It's also possible to set a default delay, applied when tasks are submitted and executed.
PausableExecutorService
,
ScheduledThreadPoolExecutor
,
setDefaultDelay(long, TimeUnit)
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
Constructor and Description |
---|
PausableScheduledThreadPoolExecutor(int corePoolSize)
Creates a new
PausableScheduledThreadPoolExecutor with the given core pool size. |
PausableScheduledThreadPoolExecutor(int corePoolSize,
RejectedExecutionHandler handler)
Creates a new
PausableScheduledThreadPoolExecutor with the given initial parameters. |
PausableScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory)
Creates a new
PausableScheduledThreadPoolExecutor with the given initial parameters. |
PausableScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
Creates a new
PausableScheduledThreadPoolExecutor with the given initial parameters. |
Modifier and Type | Method and Description |
---|---|
void |
addExecutorTerminatedListener(ExecutorTerminatedListener listener)
Adds the given
listener to the list of listeners that will be notified when the
executor terminates. |
protected void |
afterExecute(Runnable r,
Throwable t) |
protected void |
beforeExecute(Thread t,
Runnable r) |
void |
execute(Runnable command)
If no default delay was specified the
command is executed with zero required delay. |
long |
getDefaultDelay(TimeUnit unit)
Gets the default delay in the given time unit.
|
boolean |
isIncrementalDefaultDelay()
Tells whether or not the default delay is incremental.
|
void |
pause()
Pauses the executor, so that no new task will be executed until
resume() is called. |
void |
removeExecutorTerminatedListener(ExecutorTerminatedListener listener)
Removes the given
listener from the list of listeners that are notified when the
executor terminates. |
void |
resetIncrementalDefaultDelay()
Resets the incremental default delay.
|
void |
resume()
Resumes the executor, so that the awaiting tasks are executed.
|
void |
setDefaultDelay(long delay,
TimeUnit unit)
Sets the default required delay when executing/submitting tasks.
|
void |
setIncrementalDefaultDelay(boolean incremental)
Sets whether or not the default delay should be incremental, that is, increased
proportionally to each task executed.
|
void |
shutdown() |
List<Runnable> |
shutdownNow() |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> Future<T> |
submit(Runnable task,
T result) |
protected void |
terminated() |
decorateTask, decorateTask, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, toString
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
public PausableScheduledThreadPoolExecutor(int corePoolSize)
PausableScheduledThreadPoolExecutor
with the given core pool size.corePoolSize
- the number of threads to keep in the pool, even if they are idle, unless
allowCoreThreadTimeOut
is setIllegalArgumentException
- if corePoolSize < 0
public PausableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
PausableScheduledThreadPoolExecutor
with the given initial parameters.corePoolSize
- the number of threads to keep in the pool, even if they are idle, unless
allowCoreThreadTimeOut
is setthreadFactory
- the factory to use when the executor creates a new threadIllegalArgumentException
- if corePoolSize < 0
NullPointerException
- if threadFactory
is nullpublic PausableScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
PausableScheduledThreadPoolExecutor
with the given initial parameters.corePoolSize
- the number of threads to keep in the pool, even if they are idle, unless
allowCoreThreadTimeOut
is sethandler
- the handler to use when execution is blocked because the thread bounds and
queue capacities are reachedIllegalArgumentException
- if corePoolSize < 0
NullPointerException
- if handler
is nullpublic PausableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
PausableScheduledThreadPoolExecutor
with the given initial parameters.corePoolSize
- the number of threads to keep in the pool, even if they are idle, unless
allowCoreThreadTimeOut
is setthreadFactory
- the factory to use when the executor creates a new threadhandler
- the handler to use when execution is blocked because the thread bounds and
queue capacities are reachedIllegalArgumentException
- if corePoolSize < 0
NullPointerException
- if threadFactory
or handler
is nullpublic void setDefaultDelay(long delay, TimeUnit unit)
Default value is zero, no required delay.
delay
- the value delayunit
- the time unit of delayIllegalArgumentException
- if defaultDelayInMs
is negative.execute(Runnable)
,
submit(Callable)
,
submit(Runnable)
,
submit(Runnable, Object)
,
setIncrementalDefaultDelay(boolean)
public long getDefaultDelay(TimeUnit unit)
unit
- the unit that the delay will be returnedpublic void setIncrementalDefaultDelay(boolean incremental)
For example, with default delay of 500 milliseconds and incremental delay set to true
it will result in the following delays for each task:
Default value is false
.
incremental
- true
if the default delay should be incremental, false
otherwise.isIncrementalDefaultDelay()
,
setDefaultDelay(long, TimeUnit)
public boolean isIncrementalDefaultDelay()
true
if the default delay is incremental, false
otherwise.setIncrementalDefaultDelay(boolean)
public void resetIncrementalDefaultDelay()
The call to this method has no effect it the incremental default delay is not enabled.
setIncrementalDefaultDelay(boolean)
public void execute(Runnable command)
command
is executed with zero required delay.
Otherwise the default delay is applied.execute
in interface Executor
execute
in class ScheduledThreadPoolExecutor
RejectedExecutionException
- at discretion of RejectedExecutionHandler
, if the
task cannot be accepted for execution because the executor has been shut downNullPointerException
setIncrementalDefaultDelay(boolean)
,
ScheduledThreadPoolExecutor.schedule(Runnable, long, TimeUnit)
,
setDefaultDelay(long, TimeUnit)
public Future<?> submit(Runnable task)
Overridden to schedule with default delay, when non zero.
submit
in interface ExecutorService
submit
in class ScheduledThreadPoolExecutor
RejectedExecutionException
NullPointerException
setIncrementalDefaultDelay(boolean)
,
ScheduledThreadPoolExecutor.schedule(Runnable, long, TimeUnit)
,
setDefaultDelay(long, TimeUnit)
public <T> Future<T> submit(Runnable task, T result)
Overridden to schedule with default delay, when non zero.
submit
in interface ExecutorService
submit
in class ScheduledThreadPoolExecutor
RejectedExecutionException
NullPointerException
setIncrementalDefaultDelay(boolean)
,
ScheduledThreadPoolExecutor.schedule(Runnable, long, TimeUnit)
,
setDefaultDelay(long, TimeUnit)
public <T> Future<T> submit(Callable<T> task)
Overridden to schedule with default delay, when non zero.
submit
in interface ExecutorService
submit
in class ScheduledThreadPoolExecutor
RejectedExecutionException
NullPointerException
setIncrementalDefaultDelay(boolean)
,
ScheduledThreadPoolExecutor.schedule(Callable, long, TimeUnit)
,
setDefaultDelay(long, TimeUnit)
protected void beforeExecute(Thread t, Runnable r)
beforeExecute
in class ThreadPoolExecutor
protected void afterExecute(Runnable r, Throwable t)
afterExecute
in class ThreadPoolExecutor
public void pause()
PausableExecutorService
resume()
is called.pause
in interface PausableExecutorService
PausableExecutorService.resume()
public void resume()
PausableExecutorService
The call to this method has no effect if the executor is not paused.
resume
in interface PausableExecutorService
PausableExecutorService.pause()
public void shutdown()
Overridden to resume()
before shutdown()
.
shutdown
in interface ExecutorService
shutdown
in class ScheduledThreadPoolExecutor
resume()
public List<Runnable> shutdownNow()
Overridden to resume()
before shutdownNow()
.
shutdownNow
in interface ExecutorService
shutdownNow
in class ScheduledThreadPoolExecutor
resume()
protected void terminated()
terminated
in class ThreadPoolExecutor
public void addExecutorTerminatedListener(ExecutorTerminatedListener listener)
PausableExecutorService
listener
to the list of listeners that will be notified when the
executor terminates.addExecutorTerminatedListener
in interface PausableExecutorService
listener
- the listener for terminationExecutorService.isTerminated()
,
PausableExecutorService.removeExecutorTerminatedListener(ExecutorTerminatedListener)
public void removeExecutorTerminatedListener(ExecutorTerminatedListener listener)
PausableExecutorService
listener
from the list of listeners that are notified when the
executor terminates.removeExecutorTerminatedListener
in interface PausableExecutorService
listener
- the listener for terminationExecutorService.isTerminated()
,
PausableExecutorService.addExecutorTerminatedListener(ExecutorTerminatedListener)