public class SchedulerServiceLimiter extends SubmitterSchedulerLimiter implements SchedulerService
SchedulerService
. 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 SubmitterSchedulerLimiter
to add SchedulerService
features.
This does not cause any performance hits, but does require a source SchedulerService
to
rely on. If you have a SchedulerService
available this implementation should be
preferred over the SubmitterSchedulerLimiter
.
If limiting to a single thread, please see SingleThreadSchedulerSubPool
as a possible
alternative.
Constructor and Description |
---|
SchedulerServiceLimiter(SchedulerService scheduler,
int maxConcurrency)
Constructs a new limiter that implements the
SchedulerService . |
SchedulerServiceLimiter(SchedulerService scheduler,
int maxConcurrency,
boolean limitFutureListenersExecution)
Constructs a new limiter that implements the
SchedulerService . |
Modifier and Type | Method and Description |
---|---|
int |
getActiveTaskCount()
Call to check how many tasks are currently being executed in this scheduler.
|
int |
getQueuedTaskCount()
Returns how many tasks are either waiting to be executed, or are scheduled to be executed at
a future point.
|
int |
getWaitingForExecutionTaskCount()
Returns how many tasks are either waiting to be executed.
|
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.
|
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, submitScheduled
execute, getMaxConcurrency, getUnsubmittedTaskCount, setMaxConcurrency, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submitScheduled, submitScheduled, submitScheduled
submit, submit, submit
public SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency)
SchedulerService
.scheduler
- SchedulerService
implementation to submit task executions to.maxConcurrency
- maximum quantity of runnables to run in parallelpublic SchedulerServiceLimiter(SchedulerService scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
SchedulerService
.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
scheduler
- SchedulerService
implementation to submit task executions to.maxConcurrency
- maximum quantity of runnables to run in parallellimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic 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 int getActiveTaskCount()
SchedulerService
getActiveTaskCount
in interface SchedulerService
public int getQueuedTaskCount()
SchedulerService
SchedulerService.getWaitingForExecutionTaskCount()
.getQueuedTaskCount
in interface SchedulerService
public int getWaitingForExecutionTaskCount()
SchedulerService
getWaitingForExecutionTaskCount
in interface SchedulerService
public boolean isShutdown()
SchedulerService
isShutdown
in interface SchedulerService
true
if thread pool is running