Class ReservedThreadExecutor

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.thread.ReservedThreadExecutor
All Implemented Interfaces:
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.

  • Field Details

  • Constructor Details

    • ReservedThreadExecutor

      public ReservedThreadExecutor(Executor executor, int capacity)
      Parameters:
      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

    • reservedThreads

      private static int reservedThreads(Executor executor, int capacity)
      Parameters:
      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.
      Returns:
      the number of reserved threads that would be used by a ReservedThreadExecutor constructed with these arguments.
    • getExecutor

      public Executor getExecutor()
    • getCapacity

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

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

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

      @ManagedAttribute(value="idletimeout 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
      Parameters:
      idleTime - Time to wait before shrinking, or 0 for no timeout.
      idleTimeUnit - Time units for idle timeout
    • doStart

      public void doStart() throws Exception
      Overrides:
      doStart in class AbstractLifeCycle
      Throws:
      Exception
    • doStop

      public void doStop() throws Exception
      Overrides:
      doStop in class AbstractLifeCycle
      Throws:
      Exception
    • execute

      public void execute(Runnable task) throws RejectedExecutionException
      Specified by:
      execute in interface Executor
      Specified by:
      execute in interface TryExecutor
      Throws:
      RejectedExecutionException
    • 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
      Parameters:
      task - the task to run
      Returns:
      true if and only if a reserved thread was available and has been assigned the task to run.
    • startReservedThread

      private void startReservedThread()
    • toString

      public String toString()
      Overrides:
      toString in class AbstractLifeCycle