Class ExecutorLimiter

  • All Implemented Interfaces:
    java.util.concurrent.Executor, SubmitterExecutor
    Direct Known Subclasses:
    SubmitterSchedulerLimiter

    public class ExecutorLimiter
    extends java.lang.Object
    implements SubmitterExecutor
    This class is designed to limit how much parallel execution happens on a provided Executor. This allows the user 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.

    If you wish to have the tasks execute in an order other than FIFO please see OrderedExecutorLimiter.

    Since:
    4.6.0 (since 1.0.0 at org.threadly.concurrent.limiter)
    • Constructor Summary

      Constructors 
      Constructor Description
      ExecutorLimiter​(java.util.concurrent.Executor executor, int maxConcurrency)
      Construct a new execution limiter that implements the Executor interface.
      ExecutorLimiter​(java.util.concurrent.Executor executor, int maxConcurrency, boolean limitFutureListenersExecution)
      Construct a new execution limiter that implements the Executor interface.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(java.lang.Runnable task)  
      int getMaxConcurrency()
      Call to check what the maximum concurrency this limiter will allow.
      int getUnsubmittedTaskCount()
      Query how many tasks are being withheld from the parent scheduler.
      void setMaxConcurrency​(int maxConcurrency)
      Updates the concurrency limit for this limiter.
      <T> ListenableFuture<T> submit​(java.lang.Runnable task, T result)
      Submit a task to run as soon as possible.
      <T> ListenableFuture<T> submit​(java.util.concurrent.Callable<T> task)
      Submit a Callable to run as soon as possible.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExecutorLimiter

        public ExecutorLimiter​(java.util.concurrent.Executor executor,
                               int maxConcurrency)
        Construct a new execution limiter that implements the Executor interface.
        Parameters:
        executor - Executor to submit task executions to.
        maxConcurrency - maximum quantity of tasks to run in parallel
      • ExecutorLimiter

        public ExecutorLimiter​(java.util.concurrent.Executor executor,
                               int maxConcurrency,
                               boolean limitFutureListenersExecution)
        Construct a new execution limiter that implements the Executor interface.

        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:
        executor - Executor to submit task executions to.
        maxConcurrency - maximum quantity of tasks to run in parallel
        limitFutureListenersExecution - true to include listener / mapped functions towards execution limit
    • Method Detail

      • execute

        public void execute​(java.lang.Runnable task)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • submit

        public <T> ListenableFuture<T> submit​(java.lang.Runnable task,
                                              T result)
        Description copied from interface: SubmitterExecutor
        Submit a task to run as soon as possible. The Future.get() method will return the provided result once the runnable has completed.
        Specified by:
        submit in interface SubmitterExecutor
        Type Parameters:
        T - type of result for future
        Parameters:
        task - runnable to be executed
        result - result to be returned from resulting future .get() when runnable completes
        Returns:
        a future to know when the task has completed
      • submit

        public <T> ListenableFuture<T> submit​(java.util.concurrent.Callable<T> task)
        Description copied from interface: SubmitterExecutor
        Submit a Callable to run as soon as possible. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submit in interface SubmitterExecutor
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        Returns:
        a future to know when the task has completed and get the result of the callable
      • getMaxConcurrency

        public int getMaxConcurrency()
        Call to check what the maximum concurrency this limiter will allow.
        Returns:
        maximum concurrent tasks to be run
      • setMaxConcurrency

        public void setMaxConcurrency​(int maxConcurrency)
        Updates the concurrency limit for this limiter. If reducing the the limit, there will be no attempt or impact on tasks already limiting. Instead new tasks just wont be submitted to the parent pool until existing tasks complete and go below the new limit.
        Parameters:
        maxConcurrency - maximum quantity of tasks to run in parallel
        Since:
        5.4
      • getUnsubmittedTaskCount

        public int getUnsubmittedTaskCount()
        Query how many tasks are being withheld from the parent scheduler. Returning the size of the queued tasks waiting for submission to the pool.
        Returns:
        Quantity of tasks queued in this limiter