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
SingleThreadSchedulerSubPoolas 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 voidschedule(java.lang.Runnable task, long delayInMs)Schedule a one time task with a given delay.voidscheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)Schedule a fixed rate recurring task to run.voidscheduleWithFixedDelay(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 aCallablewith 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-SubmitterSchedulerimplementation 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. 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-SubmitterSchedulerimplementation to submit task executions to.maxConcurrency- maximum quantity of runnables to run in parallellimitFutureListenersExecution-trueto include listener / mapped functions towards execution limit
-
-
Method Detail
-
submitScheduled
public ListenableFuture<?> submitScheduled(java.lang.Runnable task, long delayInMs)
Description copied from interface:SubmitterSchedulerSchedule 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 returnnullonce the runnable has completed.- Specified by:
submitScheduledin 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:SubmitterSchedulerSchedule a task with a given delay. TheFuture.get()method will return the provided result once the runnable has completed.- Specified by:
submitScheduledin 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:SubmitterSchedulerSchedule aCallablewith a given delay. This is needed when a result needs to be consumed from the callable.- Specified by:
submitScheduledin 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:SubmitterSchedulerSchedule a one time task with a given delay.- Specified by:
schedulein 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:SubmitterSchedulerSchedule 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 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:SubmitterSchedulerSchedule 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 interfaceSubmitterScheduler- Parameters:
task- runnable to be executedinitialDelay- delay in milliseconds until first runperiod- amount of time in milliseconds between the start of recurring executions
-
-