Class PrioritySchedulerTaskInterceptor
- java.lang.Object
-
- org.threadly.concurrent.wrapper.interceptor.ExecutorTaskInterceptor
-
- org.threadly.concurrent.wrapper.interceptor.SubmitterSchedulerTaskInterceptor
-
- org.threadly.concurrent.wrapper.interceptor.SchedulerServiceTaskInterceptor
-
- org.threadly.concurrent.wrapper.interceptor.PrioritySchedulerTaskInterceptor
-
- All Implemented Interfaces:
java.util.concurrent.Executor,PrioritySchedulerService,SchedulerService,SubmitterExecutor,SubmitterScheduler
public class PrioritySchedulerTaskInterceptor extends SchedulerServiceTaskInterceptor implements PrioritySchedulerService
Class to wrapPrioritySchedulerServicepool so that tasks can be intercepted and either wrapped, or modified, before being submitted to the pool. This class can be passed a lambda toPrioritySchedulerTaskInterceptor(PrioritySchedulerService, BiFunction)}, orSubmitterSchedulerTaskInterceptor.wrapTask(Runnable, boolean)can be overridden to provide the task which should be submitted to thePrioritySchedulerService. Please see the javadocs ofSubmitterSchedulerTaskInterceptor.wrapTask(Runnable, boolean)for more details about ways a task can be modified or wrapped.Other variants of task wrappers:
ExecutorTaskInterceptor,SubmitterSchedulerTaskInterceptor,PrioritySchedulerTaskInterceptor.- Since:
- 4.6.0
-
-
Constructor Summary
Constructors Constructor Description PrioritySchedulerTaskInterceptor(PrioritySchedulerService parentScheduler, java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)Constructs a wrapper forPrioritySchedulerServicepool so that tasks can be intercepted and modified, before being submitted to the pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute(java.lang.Runnable task, TaskPriority priority)Executes the task as soon as possible for the given priority.TaskPrioritygetDefaultPriority()Get the default priority for the scheduler.longgetMaxWaitForLowPriority()Getter for the amount of time a low priority task will wait during thread contention before it is eligible for execution.intgetQueuedTaskCount(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.intgetWaitingForExecutionTaskCount(TaskPriority priority)Returns a count of how many tasks are either waiting to be executed for a specific priority.voidschedule(java.lang.Runnable task, long delayInMs, TaskPriority priority)Schedule a task with a given delay and a specified priority.voidscheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)Schedule a fixed rate recurring task to run.voidscheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, TaskPriority priority)Schedule a fixed delay recurring task to run.<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 aCallableto 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 aCallablewith a given delay.-
Methods inherited from class org.threadly.concurrent.wrapper.interceptor.SchedulerServiceTaskInterceptor
getActiveTaskCount, getQueuedTaskCount, getWaitingForExecutionTaskCount, isShutdown, remove, remove
-
Methods inherited from class org.threadly.concurrent.wrapper.interceptor.SubmitterSchedulerTaskInterceptor
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, wrapTask, wrapTask
-
Methods inherited from class org.threadly.concurrent.wrapper.interceptor.ExecutorTaskInterceptor
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
submit, submitScheduled
-
Methods inherited from interface org.threadly.concurrent.SchedulerService
getActiveTaskCount, getQueuedTaskCount, getWaitingForExecutionTaskCount, isShutdown, remove, remove
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit, submit, submit
-
Methods inherited from interface org.threadly.concurrent.SubmitterScheduler
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, submitScheduled
-
-
-
-
Constructor Detail
-
PrioritySchedulerTaskInterceptor
public PrioritySchedulerTaskInterceptor(PrioritySchedulerService parentScheduler, java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)
Constructs a wrapper forPrioritySchedulerServicepool so that tasks can be intercepted and modified, before being submitted to the pool.- Parameters:
parentScheduler- An instance ofPrioritySchedulerServiceto wraptaskManipulator- A lambda to manipulate aRunnablethat was submitted for execution
-
-
Method Detail
-
execute
public void execute(java.lang.Runnable task, TaskPriority priority)Description copied from interface:PrioritySchedulerServiceExecutes the task as soon as possible for the given priority.- Specified by:
executein 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:PrioritySchedulerServiceSubmit 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:
submitin 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:PrioritySchedulerServiceSubmit aCallableto run as soon as possible for the given priority. This is needed when a result needs to be consumed from the callable.- Specified by:
submitin 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:PrioritySchedulerServiceSchedule a task with a given delay and a specified priority.- Specified by:
schedulein 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:PrioritySchedulerServiceSchedule 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:
submitScheduledin 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:PrioritySchedulerServiceSchedule aCallablewith a given delay. This is needed when a result needs to be consumed from the callable.- Specified by:
submitScheduledin 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, TaskPriority priority)Description copied from interface:PrioritySchedulerServiceSchedule 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 + runtimefor the provided task.Unlike
ScheduledExecutorServiceif 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:
scheduleWithFixedDelayin interfacePrioritySchedulerService- Parameters:
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 on
-
scheduleAtFixedRate
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)Description copied from interface:PrioritySchedulerServiceSchedule 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
ScheduledExecutorServiceif 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:
scheduleAtFixedRatein interfacePrioritySchedulerService- Parameters:
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
-
getDefaultPriority
public TaskPriority getDefaultPriority()
Description copied from interface:PrioritySchedulerServiceGet the default priority for the scheduler.- Specified by:
getDefaultPriorityin interfacePrioritySchedulerService- Returns:
- the set default task priority
-
getMaxWaitForLowPriority
public long getMaxWaitForLowPriority()
Description copied from interface:PrioritySchedulerServiceGetter for the amount of time a low priority task will wait during thread contention before it is eligible for execution.- Specified by:
getMaxWaitForLowPriorityin interfacePrioritySchedulerService- Returns:
- currently set max wait for low priority task
-
getQueuedTaskCount
public int getQueuedTaskCount(TaskPriority priority)
Description copied from interface:PrioritySchedulerServiceReturns 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:
getQueuedTaskCountin 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(TaskPriority priority)
Description copied from interface:PrioritySchedulerServiceReturns 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:
getWaitingForExecutionTaskCountin interfacePrioritySchedulerService- Parameters:
priority- priority for tasks to be counted- Returns:
- quantity of tasks waiting execution
-
-