Class AbstractPriorityScheduler
- java.lang.Object
-
- org.threadly.concurrent.AbstractSubmitterExecutor
-
- org.threadly.concurrent.AbstractSubmitterScheduler
-
- org.threadly.concurrent.AbstractPriorityScheduler
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,PrioritySchedulerService
,SchedulerService
,SubmitterExecutor
,SubmitterScheduler
- Direct Known Subclasses:
NoThreadScheduler
,PriorityScheduler
,SingleThreadScheduler
,SingleThreadSchedulerSubPool
public abstract class AbstractPriorityScheduler extends AbstractSubmitterScheduler implements PrioritySchedulerService
Abstract implementation for implementations ofPrioritySchedulerService
. In general this wont be useful outside of Threadly developers, but must be a public interface since it is used in sub-packages.If you do find yourself using this class, please post an issue on github to tell us why. If there is something you want our schedulers to provide, we are happy to hear about it.
- Since:
- 4.3.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(java.lang.Runnable task, TaskPriority priority)
Executes the task as soon as possible for the given priority.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
remove(java.lang.Runnable task)
Removes the runnable task from the execution queue.boolean
remove(java.util.concurrent.Callable<?> task)
Removes the callable 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
scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
Schedule a fixed delay recurring task to run.void
setMaxWaitForLowPriority(long maxWaitForLowPriorityInMs)
Changes the max wait time for low priority tasks.<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>
submit(java.util.concurrent.Callable<T> task, TaskPriority priority)
Submit aCallable
to run as soon as possible for the given priority.<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.<T> ListenableFuture<T>
submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs, TaskPriority priority)
Schedule aCallable
with a given delay.-
Methods inherited from class org.threadly.concurrent.AbstractSubmitterScheduler
schedule, submitScheduled, submitScheduled
-
Methods inherited from class org.threadly.concurrent.AbstractSubmitterExecutor
execute, submit, submit
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.PrioritySchedulerService
scheduleAtFixedRate, scheduleWithFixedDelay, submit, submitScheduled
-
Methods inherited from interface org.threadly.concurrent.SchedulerService
getActiveTaskCount, isShutdown
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit, submit, submit
-
Methods inherited from interface org.threadly.concurrent.SubmitterScheduler
schedule, submitScheduled, submitScheduled, submitScheduled
-
-
-
-
Method Detail
-
setMaxWaitForLowPriority
public void setMaxWaitForLowPriority(long maxWaitForLowPriorityInMs)
Changes the max wait time for low priority tasks. This is the amount of time that a low priority task will wait if there are ready to execute high priority tasks. After a low priority task has waited this amount of time, it will be executed fairly with high priority tasks (meaning it will only execute the high priority task if it has been waiting longer than the low priority task).- Parameters:
maxWaitForLowPriorityInMs
- new wait time in milliseconds for low priority tasks during thread contention
-
getMaxWaitForLowPriority
public long getMaxWaitForLowPriority()
Description copied from interface:PrioritySchedulerService
Getter for the amount of time a low priority task will wait during thread contention before it is eligible for execution.- Specified by:
getMaxWaitForLowPriority
in interfacePrioritySchedulerService
- Returns:
- currently set max wait for low priority task
-
getDefaultPriority
public TaskPriority getDefaultPriority()
Description copied from interface:PrioritySchedulerService
Get the default priority for the scheduler.- Specified by:
getDefaultPriority
in interfacePrioritySchedulerService
- Returns:
- the set default task priority
-
execute
public void execute(java.lang.Runnable task, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Executes the task as soon as possible for the given priority.- Specified by:
execute
in interfacePrioritySchedulerService
- Parameters:
task
- runnable to executepriority
- priority for task to get available thread to run on
-
submit
public <T> ListenableFuture<T> submit(java.lang.Runnable task, T result, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Submit a task to run as soon as possible for the given priority. There is a slight increase in load when using submit over execute. So this should only be used when the future is necessary.The
Future.get()
method will return the provided result once the runnable has completed.- Specified by:
submit
in interfacePrioritySchedulerService
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completespriority
- priority for task to get available thread to run on- Returns:
- a future to know when the task has completed
-
submit
public <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Submit aCallable
to run as soon as possible for the given priority. This is needed when a result needs to be consumed from the callable.- Specified by:
submit
in interfacePrioritySchedulerService
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executedpriority
- priority for task to get available thread to run on- Returns:
- a future to know when the task has completed and get the result of the callable
-
schedule
public void schedule(java.lang.Runnable task, long delayInMs, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Schedule a task with a given delay and a specified priority.- Specified by:
schedule
in interfacePrioritySchedulerService
- Parameters:
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpriority
- priority for task to get available thread to run on
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Schedule a task with a given delay and a specified priority.The
Future.get()
method will return the provided result once the runnable has completed.- Specified by:
submitScheduled
in interfacePrioritySchedulerService
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- 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 on- Returns:
- a future to know when the task has completed
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs, TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Schedule aCallable
with a given delay. This is needed when a result needs to be consumed from the callable.- Specified by:
submitScheduled
in interfacePrioritySchedulerService
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executeddelayInMs
- time in milliseconds to wait to execute taskpriority
- priority for task to get available thread to run on- Returns:
- a future to know when the task has completed and get the result of the callable
-
scheduleWithFixedDelay
public void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
Description copied from interface:SubmitterScheduler
Schedule a fixed delay recurring task to run. The recurring delay time will be from the point where execution has finished. So the execution frequency is therecurringDelay + 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).- Specified by:
scheduleWithFixedDelay
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finish
-
scheduleAtFixedRate
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)
Description copied from interface:SubmitterScheduler
Schedule a fixed rate recurring task to run. The recurring delay will be the same, regardless of how long task execution takes. A given runnable will not run concurrently (unless it is submitted to the scheduler multiple times). Instead of execution takes longer than the period, the next run will occur immediately (given thread availability in the pool).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).- Specified by:
scheduleAtFixedRate
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executions
-
remove
public boolean remove(java.lang.Runnable task)
Description copied from interface:SchedulerService
Removes the runnable task from the execution queue. It is possible for the runnable to still run until this call has returned.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.- Specified by:
remove
in interfaceSchedulerService
- Parameters:
task
- The original runnable provided to the executor- Returns:
true
if the runnable was found and removed
-
remove
public boolean remove(java.util.concurrent.Callable<?> task)
Description copied from interface:SchedulerService
Removes the callable task from the execution queue. It is possible for the callable to still run until this call has returned.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.- Specified by:
remove
in interfaceSchedulerService
- Parameters:
task
- The original callable provided to the executor- Returns:
true
if the callable was found and removed
-
getQueuedTaskCount
public int getQueuedTaskCount()
Description copied from interface:SchedulerService
Returns how many tasks are either waiting to be executed, or are scheduled to be executed at a future point. This can indicate pool back pressure, but it can also just indicate generally scheduled tasks. It's computationally cheaper thanSchedulerService.getWaitingForExecutionTaskCount()
.- Specified by:
getQueuedTaskCount
in interfaceSchedulerService
- Returns:
- quantity of tasks waiting execution or scheduled to be executed later
-
getQueuedTaskCount
public int getQueuedTaskCount(TaskPriority priority)
Description copied from interface:PrioritySchedulerService
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. This can indicate pool back pressure, but it can also just indicate generally scheduled tasks. It's computationally cheaper thanPrioritySchedulerService.getWaitingForExecutionTaskCount(TaskPriority)
.- Specified by:
getQueuedTaskCount
in interfacePrioritySchedulerService
- Parameters:
priority
- priority for tasks to be counted- Returns:
- quantity of tasks waiting execution or scheduled to be executed later
-
getWaitingForExecutionTaskCount
public int getWaitingForExecutionTaskCount()
Description copied from interface:SchedulerService
Returns how many tasks are either waiting to be executed. A value here can indicate the pool is being starved for threads.- Specified by:
getWaitingForExecutionTaskCount
in interfaceSchedulerService
- Returns:
- quantity of tasks waiting execution
-
getWaitingForExecutionTaskCount
public int getWaitingForExecutionTaskCount(TaskPriority priority)
Description copied from interface:PrioritySchedulerService
Returns a count of how many tasks are either waiting to be executed for a specific priority. A value here can indicate the pool is being starved for threads.- Specified by:
getWaitingForExecutionTaskCount
in interfacePrioritySchedulerService
- Parameters:
priority
- priority for tasks to be counted- Returns:
- quantity of tasks waiting execution
-
-