Class SubmitterSchedulerLimiter
- java.lang.Object
-
- org.threadly.concurrent.wrapper.limiter.ExecutorLimiter
-
- org.threadly.concurrent.wrapper.limiter.SubmitterSchedulerLimiter
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,SubmitterExecutor
,SubmitterScheduler
- Direct Known Subclasses:
SchedulerServiceLimiter
public class SubmitterSchedulerLimiter extends ExecutorLimiter implements SubmitterScheduler
This class is designed to limit how much parallel execution happens on a providedSubmitterScheduler
. 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.- Since:
- 4.6.0 (since 4.3.0 at org.threadly.concurrent.limiter)
-
-
Constructor Summary
Constructors Constructor Description SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency)
Constructs a new limiter that implements theSubmitterScheduler
.SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
Constructs a new limiter that implements theSubmitterScheduler
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.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.<T> ListenableFuture<T>
submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
Schedule aCallable
with a given delay.-
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
-
-
-
-
Constructor Detail
-
SubmitterSchedulerLimiter
public SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency)
Constructs a new limiter that implements theSubmitterScheduler
.- Parameters:
scheduler
-SubmitterScheduler
implementation to submit task executions to.maxConcurrency
- maximum quantity of runnables to run in parallel
-
SubmitterSchedulerLimiter
public SubmitterSchedulerLimiter(SubmitterScheduler scheduler, int maxConcurrency, boolean limitFutureListenersExecution)
Constructs a new limiter that implements theSubmitterScheduler
.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. Specifyingfalse
will release the limit as soon as the original task completes. Specifyingtrue
will continue to enforce the limit until all listeners (without an executor) complete.- Parameters:
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 limit
-
-
Method Detail
-
submitScheduled
public ListenableFuture<?> submitScheduled(java.lang.Runnable task, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule a task with a given delay. There is a slight increase in load when usingSubmitterScheduler.submitScheduled(Runnable, long)
overSubmitterScheduler.schedule(Runnable, long)
. So this should only be used when the future is necessary.The
Future.get()
method will returnnull
once the runnable has completed.- Specified by:
submitScheduled
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute task- Returns:
- a future to know when the task has completed
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule a task with a given delay. TheFuture.get()
method will return the provided result once the runnable has completed.- Specified by:
submitScheduled
in interfaceSubmitterScheduler
- 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 task- Returns:
- a future to know when the task has completed
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule aCallable
with a given delay. This is needed when a result needs to be consumed from the callable.- Specified by:
submitScheduled
in interfaceSubmitterScheduler
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executeddelayInMs
- time in milliseconds to wait to execute task- 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)
Description copied from interface:SubmitterScheduler
Schedule a one time task with a given delay.- Specified by:
schedule
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute task
-
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
-
-