Class ReservedThreadExecutor

All Implemented Interfaces:
Executor, Dumpable, LifeCycle, TryExecutor

@ManagedObject("A pool for reserved threads") public class ReservedThreadExecutor extends AbstractLifeCycle implements TryExecutor, Dumpable

A TryExecutor using pre-allocated/reserved threads from an external Executor.

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

Threads are reserved lazily, with new reserved threads being allocated from the external Executor passed to the constructor. Whenever 1 or more reserved threads have been idle for more than getIdleTimeoutMs() then one reserved thread will return to the external Executor.

  • Constructor Details

    • ReservedThreadExecutor

      public ReservedThreadExecutor(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 Details

    • getExecutor

      public Executor getExecutor()
    • getCapacity

      @ManagedAttribute(value="max number of reserved threads", readonly=true) public int getCapacity()
      the maximum number of reserved threads
    • getAvailable

      @ManagedAttribute(value="available reserved threads", readonly=true) public int getAvailable()
      the number of threads available to tryExecute(Runnable)
    • getPending

      @ManagedAttribute(value="pending reserved threads", readonly=true) public int getPending()
    • getIdleTimeoutMs

      @ManagedAttribute(value="idle timeout in ms", readonly=true) public long getIdleTimeoutMs()
    • setIdleTimeout

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

      public void doStart() throws Exception
      Description copied from class: AbstractLifeCycle
      Method to override to start the lifecycle
      doStart in class AbstractLifeCycle
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • doStop

      public void doStop() throws Exception
      Description copied from class: AbstractLifeCycle
      Method to override to stop the lifecycle
      doStop in class AbstractLifeCycle
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • execute

      public void execute(Runnable task) throws RejectedExecutionException
      Specified by:
      execute in interface Executor
      Specified by:
      execute in interface TryExecutor
    • tryExecute

      public boolean tryExecute(Runnable task)

      Executes the given task if and only if a reserved thread is available.

      Specified by:
      tryExecute in interface TryExecutor
      task - the task to run
      true if and only if a reserved thread was available and has been assigned the task to run.
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
      Specified by:
      dump in interface Dumpable
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      IOException - if unable to write to Appendable
    • toString

      public String toString()
      toString in class AbstractLifeCycle