Class 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 provided 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.

    Since:
    4.6.0 (since 4.3.0 at org.threadly.concurrent.limiter)
    • Constructor Detail

      • SubmitterSchedulerLimiter

        public SubmitterSchedulerLimiter​(SubmitterScheduler scheduler,
                                         int maxConcurrency)
        Constructs a new limiter that implements the SubmitterScheduler.
        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 the 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.

        Parameters:
        scheduler - SubmitterScheduler implementation to submit task executions to.
        maxConcurrency - maximum quantity of runnables to run in parallel
        limitFutureListenersExecution - true to include listener / mapped functions towards execution limit
    • Method Detail

      • 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. The Future.get() method will return the provided result once the runnable has completed.
        Specified by:
        submitScheduled in interface SubmitterScheduler
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - runnable to execute
        result - result to be returned from resulting future .get() when runnable completes
        delayInMs - 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 a Callable with a given delay. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submitScheduled in interface SubmitterScheduler
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        delayInMs - 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 interface SubmitterScheduler
        Parameters:
        task - runnable to execute
        delayInMs - 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 the 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).

        Specified by:
        scheduleWithFixedDelay in interface SubmitterScheduler
        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        recurringDelay - 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 interface SubmitterScheduler
        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        period - amount of time in milliseconds between the start of recurring executions