public class PriorityScheduler extends AbstractPriorityScheduler
ScheduledThreadPoolExecutor
this scheduled executor's pool size
can shrink if set with a lower value via setPoolSize(int)
. It also has the benefit
that you can provide "low priority" tasks.
These low priority tasks will delay their execution if there are other high priority tasks ready to run, as long as they have not exceeded their maximum wait time. If they have exceeded their maximum wait time, and high priority tasks delay time is less than the low priority delay time, then those low priority tasks will be executed. What this results in is a task which has lower priority, but which wont be starved from execution.
Most tasks provided into this pool will likely want to be "high priority", to more closely
match the behavior of other thread pools. That is why unless specified by the constructor, the
default TaskPriority
is High.
In all conditions, "low priority" tasks will never be starved. This makes "low priority" tasks
ideal which do regular cleanup, or in general anything that must run, but cares little if there
is a 1, or 10 second gap in the execution time. That amount of tolerance is adjustable by
setting the maxWaitForLowPriorityInMs
either in the constructor, or at runtime via
AbstractPriorityScheduler.setMaxWaitForLowPriority(long)
.
Constructor and Description |
---|
PriorityScheduler(int poolSize)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
PriorityScheduler(int poolSize,
boolean useDaemonThreads)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
PriorityScheduler(int poolSize,
TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
PriorityScheduler(int poolSize,
TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs,
boolean useDaemonThreads)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
PriorityScheduler(int poolSize,
TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs,
boolean stavableStartsThreads,
java.util.concurrent.ThreadFactory threadFactory)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
PriorityScheduler(int poolSize,
TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs,
java.util.concurrent.ThreadFactory threadFactory)
Constructs a new thread pool, though threads will be lazily started as it has tasks ready to
run.
|
Modifier and Type | Method and Description |
---|---|
void |
adjustPoolSize(int delta)
Adjust the pools size by a given delta.
|
void |
awaitTermination()
Block until the thread pool has shutdown and all threads have been stopped.
|
boolean |
awaitTermination(long timeoutMillis)
Block until the thread pool has shutdown and all threads have been stopped.
|
int |
getActiveTaskCount()
Call to check how many tasks are currently being executed in this thread pool.
|
int |
getCurrentPoolSize()
Getter for the current quantity of threads running in this pool (either active or idle).
|
int |
getMaxPoolSize()
Getter for the currently set max thread pool size.
|
int |
getQueuedTaskCount()
Returns how many tasks are either waiting to be executed, or are scheduled to be executed at
a future point.
|
int |
getQueuedTaskCount(TaskPriority priority)
Returns a count of how many tasks are either waiting to be executed, or are scheduled to be
executed at a future point for a specific priority.
|
boolean |
isShutdown()
Function to check if the thread pool is currently accepting and handling tasks.
|
void |
prestartAllThreads()
Ensures all threads have been started, it will create threads till the thread count matches
the set pool size (checked via
getMaxPoolSize() ). |
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period,
TaskPriority priority)
Schedule a fixed rate recurring task to run.
|
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay,
TaskPriority priority)
Schedule a fixed delay recurring task to run.
|
void |
setPoolSize(int newPoolSize)
Change the set thread pool size.
|
void |
shutdown()
Stops any new tasks from being submitted to the pool.
|
java.util.List<java.lang.Runnable> |
shutdownNow()
Stops any new tasks from being able to be executed and removes workers from the pool.
|
execute, getDefaultPriority, getMaxWaitForLowPriority, getWaitingForExecutionTaskCount, getWaitingForExecutionTaskCount, remove, remove, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setMaxWaitForLowPriority, submit, submit, submitScheduled, submitScheduled
schedule, submitScheduled, submitScheduled
execute, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit, submitScheduled
schedule, submitScheduled, submitScheduled, submitScheduled
submit, submit, submit
public PriorityScheduler(int poolSize)
poolSize
- Thread pool size that should be maintainedpublic PriorityScheduler(int poolSize, boolean useDaemonThreads)
poolSize
- Thread pool size that should be maintaineduseDaemonThreads
- true
if newly created threads should be daemonpublic PriorityScheduler(int poolSize, TaskPriority defaultPriority, long maxWaitForLowPriorityInMs)
poolSize
- Thread pool size that should be maintaineddefaultPriority
- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs
- time low priority tasks to wait if there are high priority tasks ready to runpublic PriorityScheduler(int poolSize, TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, boolean useDaemonThreads)
poolSize
- Thread pool size that should be maintaineddefaultPriority
- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs
- time low priority tasks to wait if there are high priority tasks ready to runuseDaemonThreads
- true
if newly created threads should be daemonpublic PriorityScheduler(int poolSize, TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, java.util.concurrent.ThreadFactory threadFactory)
poolSize
- Thread pool size that should be maintaineddefaultPriority
- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs
- time low priority tasks to wait if there are high priority tasks ready to runthreadFactory
- thread factory for producing new threads within executorpublic PriorityScheduler(int poolSize, TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, boolean stavableStartsThreads, java.util.concurrent.ThreadFactory threadFactory)
poolSize
- Thread pool size that should be maintaineddefaultPriority
- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs
- time low priority tasks to wait if there are high priority tasks ready to runstavableStartsThreads
- true
to have TaskPriority.Starvable tasks start new threadsthreadFactory
- thread factory for producing new threads within executorpublic int getMaxPoolSize()
public int getCurrentPoolSize()
getMaxPoolSize()
in that we
lazily create threads. This represents the amount of threads needed to be created so far,
where getMaxPoolSize()
represents the amount of threads the pool may grow to.public void setPoolSize(int newPoolSize)
If the value is less than the current running threads, as threads finish they will exit rather than accept new tasks. No currently running tasks will be interrupted, rather we will just wait for them to finish before killing the thread.
If this is an increase in the pool size, threads will be lazily started as needed till the new size is reached. If there are tasks waiting for threads to run on, they immediately will be started.
newPoolSize
- New core pool size, must be at least onepublic void adjustPoolSize(int delta)
IllegalStateException
will be thrown.delta
- Delta to adjust the max pool size bypublic int getActiveTaskCount()
getCurrentPoolSize()
, this count will NOT include idle threads waiting to execute
tasks.public void prestartAllThreads()
getMaxPoolSize()
). These new threads will remain
idle till there is tasks ready to execute.public boolean isShutdown()
SchedulerService
true
if thread pool is runningpublic void shutdown()
shutdown()
or
shutdownNow()
has already been called, this will have no effect.
If you wish to not want to run any queued tasks you should use shutdownNow()
.
public java.util.List<java.lang.Runnable> shutdownNow()
This implementation refuses new submissions after this call. And will NOT interrupt any tasks which are currently running. However any tasks which are waiting in queue to be run (but have not started yet), will not be run. Those waiting tasks will be removed, and as workers finish with their current tasks the threads will be joined.
public void awaitTermination() throws java.lang.InterruptedException
shutdown()
or shutdownNow()
is invoked, then this will block forever.java.lang.InterruptedException
- Thrown if blocking thread is interrupted waiting for shutdownpublic boolean awaitTermination(long timeoutMillis) throws java.lang.InterruptedException
shutdown()
or shutdownNow()
is invoked, then this will block until the
timeout is reached.timeoutMillis
- time to block and wait for thread pool to shutdowntrue
if the pool has shutdown, false if timeout was reachedjava.lang.InterruptedException
- Thrown if blocking thread is interrupted waiting for shutdownpublic int getQueuedTaskCount()
SchedulerService
SchedulerService.getWaitingForExecutionTaskCount()
.getQueuedTaskCount
in interface SchedulerService
getQueuedTaskCount
in class AbstractPriorityScheduler
public int getQueuedTaskCount(TaskPriority priority)
PrioritySchedulerService
PrioritySchedulerService.getWaitingForExecutionTaskCount(TaskPriority)
.getQueuedTaskCount
in interface PrioritySchedulerService
getQueuedTaskCount
in class AbstractPriorityScheduler
priority
- priority for tasks to be countedpublic void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, TaskPriority priority)
PrioritySchedulerService
recurringDelay + runtime
for the provided task.
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finishpriority
- priority for task to get available thread to run onpublic void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)
PrioritySchedulerService
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executionspriority
- priority for task to get available thread to run on