Class ReservedThreadExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Executor, LifeCycle, TryExecutor

    @ManagedObject("A pool for reserved threads")
    public class ReservedThreadExecutor
    extends AbstractLifeCycle
    implements TryExecutor
    An Executor using preallocated/reserved Threads from a wrapped Executor.

    Calls to execute(Runnable) on a ReservedThreadExecutor will either succeed with a Thread immediately being assigned the Runnable task, or fail if no Thread is available.

    Threads are reserved lazily, with a new reserved thread being allocated from a wrapped Executor when an execution fails. If the setIdleTimeout(long, TimeUnit) is set to non zero (default 1 minute), then the reserved thread pool will shrink by 1 thread whenever it has been idle for that period.

    • Constructor Detail

      • ReservedThreadExecutor

        public ReservedThreadExecutor​(java.util.concurrent.Executor executor,
                                      int capacity)
        executor - The executor to use to obtain threads
        capacity - The number of threads to preallocate. If less than 0 then capacity is calculated based on a heuristic from the number of available processors and thread pool size.
    • Method Detail

      • getExecutor

        public java.util.concurrent.Executor getExecutor()
      • setIdleTimeout

        public void setIdleTimeout​(long idleTime,
                                   java.util.concurrent.TimeUnit idleTimeUnit)
        Set the idle timeout for shrinking the reserved thread pool
        idleTime - Time to wait before shrinking, or 0 for no timeout.
        idleTimeUnit - Time units for idle timeout
      • doStart

        public void doStart()
                     throws java.lang.Exception
        doStart in class AbstractLifeCycle
      • doStop

        public void doStop()
                    throws java.lang.Exception
        doStop in class AbstractLifeCycle
      • execute

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

        public boolean tryExecute​(java.lang.Runnable task)
        Description copied from interface: TryExecutor
        Attempt to execute a task.
        Specified by:
        tryExecute in interface TryExecutor
        task - The task to run
        True iff a reserved thread was available and has been assigned the task to run.