public class SubmitterSchedulerLimiter extends ExecutorLimiter implements SubmitterScheduler
SubmitterScheduler
. 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.
If limiting to a single thread, please see SingleThreadSchedulerSubPool
as a possible
alternative.
Constructor and Description |
---|
SubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency)
Constructs a new limiter that implements the
SubmitterScheduler . |
SubmitterSchedulerLimiter(SubmitterScheduler scheduler,
int maxConcurrency,
boolean limitFutureListenersExecution)
Constructs a new limiter that implements the
SubmitterScheduler . |
Modifier and Type | Method and Description |
---|---|
void |
schedule(java.lang.Runnable task,
long delayInMs)
Schedule a one time task with a given delay.
|
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.
|
<T> ListenableFuture<T> |
submitScheduled(java.util.concurrent.Callable<T> task,
long delayInMs)
Schedule a
Callable with a given delay. |
ListenableFuture<?> |
submitScheduled(java.lang.Runnable task,
long delayInMs)
Schedule a task with a given delay.
|
<T> ListenableFuture<T> |
submitScheduled(java.lang.Runnable task,
T result,
long delayInMs)
Schedule a task with a given delay.
|
execute, getMaxConcurrency, getUnsubmittedTaskCount, setMaxConcurrency, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit, submit, submit
public SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency)
SubmitterScheduler
.scheduler
- SubmitterScheduler
implementation to submit task executions to.maxConcurrency
- maximum quantity of runnables to run in parallelpublic SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
SubmitterScheduler
.
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
- SubmitterScheduler
implementation to submit task executions to.maxConcurrency
- maximum quantity of runnables to run in parallellimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic ListenableFuture<?> submitScheduled(java.lang.Runnable task, long delayInMs)
SubmitterScheduler
SubmitterScheduler.submitScheduled(Runnable, long)
over SubmitterScheduler.schedule(Runnable, long)
. So this
should only be used when the future is necessary.
The Future.get()
method will return null
once the runnable has
completed.
submitScheduled
in interface SubmitterScheduler
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpublic <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs)
SubmitterScheduler
Future.get()
method will return
the provided result once the runnable has completed.submitScheduled
in interface SubmitterScheduler
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 taskpublic <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
SubmitterScheduler
Callable
with a given delay. This is needed when a result needs to be
consumed from the callable.submitScheduled
in interface SubmitterScheduler
T
- type of result returned from the futuretask
- callable to be executeddelayInMs
- time in milliseconds to wait to execute taskpublic void schedule(java.lang.Runnable task, long delayInMs)
SubmitterScheduler
schedule
in interface SubmitterScheduler
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpublic 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 executions