Class SchedulerServiceLimiter
- java.lang.Object
-
- org.threadly.concurrent.wrapper.limiter.ExecutorLimiter
-
- org.threadly.concurrent.wrapper.limiter.SubmitterSchedulerLimiter
-
- org.threadly.concurrent.wrapper.limiter.SchedulerServiceLimiter
-
- All Implemented Interfaces:
java.util.concurrent.Executor,SchedulerService,SubmitterExecutor,SubmitterScheduler
public class SchedulerServiceLimiter extends SubmitterSchedulerLimiter implements SchedulerService
This class is designed to limit how much parallel execution happens on a providedSchedulerService. This allows the implementor to have one thread pool for all their code, and if they want certain sections to have less levels of parallelism (possibly because those those sections would completely consume the global pool), they can wrap the executor in this class.Thus providing you better control on the absolute thread count and how much parallelism can occur in different sections of the program.
This is an alternative from having to create multiple thread pools. By using this you also are able to accomplish more efficiently thread use than multiple thread pools would.
This extends the
SubmitterSchedulerLimiterto addSchedulerServicefeatures. This does not cause any performance hits, but does require a sourceSchedulerServiceto rely on. If you have aSchedulerServiceavailable this implementation should be preferred over theSubmitterSchedulerLimiter.If limiting to a single thread, please see
SingleThreadSchedulerSubPoolas a possible alternative.- Since:
- 4.6.0 (since 2.0.0 at org.threadly.concurrent.limiter)
-
-
Constructor Summary
Constructors Constructor Description SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency)Constructs a new limiter that implements theSchedulerService.SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency, boolean limitFutureListenersExecution)Constructs a new limiter that implements theSchedulerService.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetActiveTaskCount()Call to check how many tasks are currently being executed in this scheduler.intgetQueuedTaskCount()Returns how many tasks are either waiting to be executed, or are scheduled to be executed at a future point.intgetWaitingForExecutionTaskCount()Returns how many tasks are either waiting to be executed.booleanisShutdown()Function to check if the thread pool is currently accepting and handling tasks.booleanremove(java.lang.Runnable task)Removes the runnable task from the execution queue.booleanremove(java.util.concurrent.Callable<?> task)Removes the callable task from the execution queue.-
Methods inherited from class org.threadly.concurrent.wrapper.limiter.SubmitterSchedulerLimiter
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, submitScheduled
-
Methods inherited from class org.threadly.concurrent.wrapper.limiter.ExecutorLimiter
execute, getMaxConcurrency, getUnsubmittedTaskCount, setMaxConcurrency, 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.SubmitterExecutor
submit, submit, submit
-
Methods inherited from interface org.threadly.concurrent.SubmitterScheduler
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, submitScheduled
-
-
-
-
Constructor Detail
-
SchedulerServiceLimiter
public SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency)
Constructs a new limiter that implements theSchedulerService.- Parameters:
scheduler-SchedulerServiceimplementation to submit task executions to.maxConcurrency- maximum quantity of runnables to run in parallel
-
SchedulerServiceLimiter
public SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
Constructs a new limiter that implements theSchedulerService.This constructor allows you to specify if listeners /
FutureCallback's / functions inListenableFuture.map(java.util.function.Function)orListenableFuture.flatMap(java.util.function.Function)should be counted towards the concurrency limit. Specifyingfalsewill release the limit as soon as the original task completes. Specifyingtruewill continue to enforce the limit until all listeners (without an executor) complete.- Parameters:
scheduler-SchedulerServiceimplementation to submit task executions to.maxConcurrency- maximum quantity of runnables to run in parallellimitFutureListenersExecution-trueto include listener / mapped functions towards execution limit
-
-
Method Detail
-
remove
public boolean remove(java.lang.Runnable task)
Description copied from interface:SchedulerServiceRemoves 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:
removein interfaceSchedulerService- Parameters:
task- The original runnable provided to the executor- Returns:
trueif the runnable was found and removed
-
remove
public boolean remove(java.util.concurrent.Callable<?> task)
Description copied from interface:SchedulerServiceRemoves 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:
removein interfaceSchedulerService- Parameters:
task- The original callable provided to the executor- Returns:
trueif the callable was found and removed
-
getActiveTaskCount
public int getActiveTaskCount()
Description copied from interface:SchedulerServiceCall to check how many tasks are currently being executed in this scheduler.- Specified by:
getActiveTaskCountin interfaceSchedulerService- Returns:
- current number of running tasks
-
getQueuedTaskCount
public int getQueuedTaskCount()
Description copied from interface:SchedulerServiceReturns 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:
getQueuedTaskCountin interfaceSchedulerService- Returns:
- quantity of tasks waiting execution or scheduled to be executed later
-
getWaitingForExecutionTaskCount
public int getWaitingForExecutionTaskCount()
Description copied from interface:SchedulerServiceReturns how many tasks are either waiting to be executed. A value here can indicate the pool is being starved for threads.- Specified by:
getWaitingForExecutionTaskCountin interfaceSchedulerService- Returns:
- quantity of tasks waiting execution
-
isShutdown
public boolean isShutdown()
Description copied from interface:SchedulerServiceFunction to check if the thread pool is currently accepting and handling tasks.- Specified by:
isShutdownin interfaceSchedulerService- Returns:
trueif thread pool is running
-
-