Class ExecutorQueueLimitRejector

  • All Implemented Interfaces:
    java.util.concurrent.Executor, SubmitterExecutor

    public class ExecutorQueueLimitRejector
    extends AbstractSubmitterExecutor
    A simple way to limit any Executor so that queues are managed. In addition this queue is tracked completely independent of the Executor's actual queue, so these can be distributed in code to limit queues differently to different parts of the system, while letting them all back the same Executor.

    Once the limit has been reached, if additional tasks are supplied a RejectedExecutionException will be thrown. This is the threadly equivalent of supplying a limited sized blocking queue to a java.util.concurrent thread pool.

    See SubmitterSchedulerQueueLimitRejector, SchedulerServiceQueueLimitRejector and PrioritySchedulerServiceQueueLimitRejector as other possible implementations.

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

      • ExecutorQueueLimitRejector

        public ExecutorQueueLimitRejector​(java.util.concurrent.Executor parentExecutor,
                                          int queuedTaskLimit)
        Constructs a new ExecutorQueueLimitRejector with the provided scheduler and limit.
        Parameters:
        parentExecutor - Executor to execute tasks on to
        queuedTaskLimit - Maximum number of queued tasks before executions should be rejected
      • ExecutorQueueLimitRejector

        public ExecutorQueueLimitRejector​(java.util.concurrent.Executor parentExecutor,
                                          int queuedTaskLimit,
                                          RejectedExecutionHandler rejectedExecutionHandler)
        Constructs a new ExecutorQueueLimitRejector with the provided scheduler and limit.
        Parameters:
        parentExecutor - Executor to execute tasks on to
        queuedTaskLimit - Maximum number of queued tasks before executions should be rejected
        rejectedExecutionHandler - Handler to accept tasks which could not be executed due to queue size
        Since:
        4.8.0
    • Method Detail

      • getQueuedTaskCount

        public int getQueuedTaskCount()
        Invoked to check how many tasks are currently being tracked as queued by this limiter.
        Returns:
        Number of tracked tasks waiting for execution to start
      • getQueueLimit

        public int getQueueLimit()
        Invoked to check the currently set queue limit.
        Returns:
        Maximum number of tasks allowed to queue in the parent executor
      • setQueueLimit

        public void setQueueLimit​(int newLimit)
        Invoked to change the set limit. Negative and zero limits are allowed, but they will cause all executions to be rejected. If set below the current queue size, those tasks will still remain queued for execution.
        Parameters:
        newLimit - New limit to avoid executions