public class PriorityDelegatingScheduler extends AbstractSubmitterScheduler implements PrioritySchedulerService
PrioritySchedulerService
with a collection of multiple
executors (and queues) for each priority rather than a single queue.
One useful example of this could be in producing a priority aware implementation of
SchedulerServiceLimiter
where different
priorities are allowed to use a given portion of the pool.
Because of the use of multiple pools (often times backed by a single pool), this class does have
some unique considerations. Most of the non-task execution specific functions of
PrioritySchedulerService
will have some unique behaviors. Specifically
getMaxWaitForLowPriority()
will always return zero because there is no ability to
balance the task queues between the different schedulers. In addition functions like
getActiveTaskCount()
, getQueuedTaskCount()
,
getWaitingForExecutionTaskCount()
will return the values from the default priority's
scheduler (since it is assumed that the provided schedulers may be multiple of the same reference,
or may delegate to the same parent pool).
Constructor and Description |
---|
PriorityDelegatingScheduler(SchedulerService highPriorityScheduler,
SchedulerService lowPriorityScheduler,
SchedulerService starvablePriorityScheduler,
TaskPriority defaultPriority)
Construct a new
PrioritySchedulerService that delegates to the given schedulers. |
Modifier and Type | Method and Description |
---|---|
void |
execute(java.lang.Runnable task,
TaskPriority priority)
Executes the task as soon as possible for the given priority.
|
int |
getActiveTaskCount()
Call to check how many tasks are currently being executed in this scheduler.
|
TaskPriority |
getDefaultPriority()
Get the default priority for the scheduler.
|
long |
getMaxWaitForLowPriority()
Getter for the amount of time a low priority task will wait during thread contention before
it is eligible for execution.
|
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.
|
int |
getWaitingForExecutionTaskCount()
Returns how many tasks are either waiting to be executed.
|
int |
getWaitingForExecutionTaskCount(TaskPriority priority)
Returns a count of how many tasks are either waiting to be executed for a specific priority.
|
boolean |
isShutdown()
Function to check if the thread pool is currently accepting and handling tasks.
|
boolean |
remove(java.util.concurrent.Callable<?> task)
Removes the callable task from the execution queue.
|
boolean |
remove(java.lang.Runnable task)
Removes the runnable task from the execution queue.
|
void |
schedule(java.lang.Runnable task,
long delayInMs,
TaskPriority priority)
Schedule a task with a given delay and a specified priority.
|
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period)
Schedule a fixed rate recurring task to run.
|
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)
Schedule a fixed delay recurring task to run.
|
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay,
TaskPriority priority)
Schedule a fixed delay recurring task to run.
|
<T> ListenableFuture<T> |
submit(java.util.concurrent.Callable<T> task,
TaskPriority priority)
Submit a
Callable to run as soon as possible for the given priority. |
<T> ListenableFuture<T> |
submit(java.lang.Runnable task,
T result,
TaskPriority priority)
Submit a task to run as soon as possible for the given priority.
|
<T> ListenableFuture<T> |
submitScheduled(java.util.concurrent.Callable<T> task,
long delayInMs,
TaskPriority priority)
Schedule a
Callable with a given delay. |
<T> ListenableFuture<T> |
submitScheduled(java.lang.Runnable task,
T result,
long delayInMs,
TaskPriority priority)
Schedule a task with a given delay and a specified priority.
|
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 PriorityDelegatingScheduler(SchedulerService highPriorityScheduler, SchedulerService lowPriorityScheduler, SchedulerService starvablePriorityScheduler, TaskPriority defaultPriority)
PrioritySchedulerService
that delegates to the given schedulers.
Please see class documentation to understand how defaultPriority
will also provide the
priority for the non-task execution portions of the SchedulerService
interface.
highPriorityScheduler
- Scheduler to be used for high priority taskslowPriorityScheduler
- Scheduler to be used for low priority tasksstarvablePriorityScheduler
- Scheduler to be used for starvable priority tasks,
or null
to delegate to lowPriorityScheduler
defaultPriority
- The default priority to be used when none is providedpublic TaskPriority getDefaultPriority()
PrioritySchedulerService
getDefaultPriority
in interface PrioritySchedulerService
public long getMaxWaitForLowPriority()
PrioritySchedulerService
getMaxWaitForLowPriority
in interface PrioritySchedulerService
public int getQueuedTaskCount(TaskPriority priority)
PrioritySchedulerService
PrioritySchedulerService.getWaitingForExecutionTaskCount(TaskPriority)
.getQueuedTaskCount
in interface PrioritySchedulerService
priority
- priority for tasks to be countedpublic int getWaitingForExecutionTaskCount(TaskPriority priority)
PrioritySchedulerService
getWaitingForExecutionTaskCount
in interface PrioritySchedulerService
priority
- priority for tasks to be countedpublic boolean isShutdown()
SchedulerService
isShutdown
in interface SchedulerService
true
if thread pool is runningpublic int getQueuedTaskCount()
SchedulerService
SchedulerService.getWaitingForExecutionTaskCount()
.getQueuedTaskCount
in interface SchedulerService
public int getWaitingForExecutionTaskCount()
SchedulerService
getWaitingForExecutionTaskCount
in interface SchedulerService
public int getActiveTaskCount()
SchedulerService
getActiveTaskCount
in interface SchedulerService
public boolean remove(java.lang.Runnable task)
SchedulerService
Note that this call has high guarantees on the ability to remove the task (as in a complete guarantee). But while this is being invoked, it will reduce the throughput of execution, so should NOT be used extremely frequently.
For non-recurring tasks using a future and calling
Future.cancel(boolean)
can be a better solution.
remove
in interface SchedulerService
task
- The original runnable provided to the executortrue
if the runnable was found and removedpublic boolean remove(java.util.concurrent.Callable<?> task)
SchedulerService
Note that this call has high guarantees on the ability to remove the task (as in a complete guarantee). But while this is being invoked, it will reduce the throughput of execution, so should NOT be used extremely frequently.
For non-recurring tasks using a future and calling
Future.cancel(boolean)
can be a better solution.
remove
in interface SchedulerService
task
- The original callable provided to the executortrue
if the callable was found and removedpublic void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
SubmitterScheduler
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).
scheduleWithFixedDelay
in interface SubmitterScheduler
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finishpublic void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)
SubmitterScheduler
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).
scheduleAtFixedRate
in interface SubmitterScheduler
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executionspublic void execute(java.lang.Runnable task, TaskPriority priority)
PrioritySchedulerService
execute
in interface PrioritySchedulerService
task
- runnable to executepriority
- priority for task to get available thread to run onpublic <T> ListenableFuture<T> submit(java.lang.Runnable task, T result, TaskPriority priority)
PrioritySchedulerService
The Future.get()
method will return the provided result once the runnable has
completed.
submit
in interface PrioritySchedulerService
T
- type of result returned from the futuretask
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completespriority
- priority for task to get available thread to run onpublic <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task, TaskPriority priority)
PrioritySchedulerService
Callable
to run as soon as possible for the given priority. This is needed
when a result needs to be consumed from the callable.submit
in interface PrioritySchedulerService
T
- type of result returned from the futuretask
- callable to be executedpriority
- priority for task to get available thread to run onpublic void schedule(java.lang.Runnable task, long delayInMs, TaskPriority priority)
PrioritySchedulerService
schedule
in interface PrioritySchedulerService
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpriority
- priority for task to get available thread to run onpublic <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs, TaskPriority priority)
PrioritySchedulerService
The Future.get()
method will return the provided result once the runnable
has completed.
submitScheduled
in interface PrioritySchedulerService
T
- type of result returned from the futuretask
- runnable to executeresult
- result to be returned from resulting future .get() when runnable completesdelayInMs
- time in milliseconds to wait to execute taskpriority
- priority for task to get available thread to run onpublic <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs, TaskPriority priority)
PrioritySchedulerService
Callable
with a given delay. This is needed when a result needs to be
consumed from the callable.submitScheduled
in interface PrioritySchedulerService
T
- type of result returned from the futuretask
- callable to be executeddelayInMs
- time in milliseconds to wait to execute taskpriority
- priority for task to get available thread to run onpublic 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).
scheduleWithFixedDelay
in interface PrioritySchedulerService
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).
scheduleAtFixedRate
in interface PrioritySchedulerService
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