Class VirtualThreadPool

All Implemented Interfaces:
Executor, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, ThreadPool, TryExecutor, VirtualThreads.Configurable

@ManagedObject("A thread non-pool for virtual threads") public class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool, Dumpable, TryExecutor, VirtualThreads.Configurable

An implementation of ThreadPool interface that does not pool, but instead uses VirtualThreads.

It is possible to specify the max number of concurrent virtual threads that can be spawned, to help limiting resource usage in applications, especially in case of load spikes, where an unlimited number of virtual threads may be spawned, compete for resources, and eventually bring the system down due to memory exhaustion.

  • Constructor Details

    • VirtualThreadPool

      public VirtualThreadPool()
    • VirtualThreadPool

      public VirtualThreadPool(int maxThreads)
  • Method Details

    • getName

      @ManagedAttribute("name of the thread pool") public String getName()
      Returns:
      the name of this thread pool
    • setName

      public void setName(String name)

      Sets the name of this thread pool, used as a prefix for the thread names.

      Parameters:
      name - the name of this thread pool
    • getMaxThreads

      @ManagedAttribute("The max number of concurrent virtual threads") public int getMaxThreads()
      Returns:
      the maximum number of concurrent virtual threads
    • setMaxThreads

      public void setMaxThreads(int maxThreads)
      Parameters:
      maxThreads - the maximum number of concurrent virtual threads
    • isTracking

      @ManagedAttribute("Whether virtual threads are tracked") public boolean isTracking()
      Get if this pool is tracking virtual threads.
      Returns:
      true if the virtual threads will be tracked.
      See Also:
    • setTracking

      public void setTracking(boolean tracking)
    • isDetailedDump

      @ManagedAttribute("Whether to report additional details in the dump") public boolean isDetailedDump()
    • setDetailedDump

      public void setDetailedDump(boolean detailedDump)
    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      Overrides:
      doStart in class ContainerLifeCycle
      Throws:
      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

      protected void doStop() throws Exception
      Description copied from class: ContainerLifeCycle
      Stops the managed lifecycle beans in the reverse order they were added.
      Overrides:
      doStop in class ContainerLifeCycle
      Throws:
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • getVirtualThreadsExecutor

      public Executor getVirtualThreadsExecutor()
      Specified by:
      getVirtualThreadsExecutor in interface VirtualThreads.Configurable
      Returns:
      the Executor to use to execute tasks in virtual threads
    • setVirtualThreadsExecutor

      public void setVirtualThreadsExecutor(Executor executor)
      Specified by:
      setVirtualThreadsExecutor in interface VirtualThreads.Configurable
      Parameters:
      executor - the Executor to use to execute tasks in virtual threads
      See Also:
    • join

      public void join() throws InterruptedException
      Description copied from interface: ThreadPool
      Blocks until the thread pool is stopped.
      Specified by:
      join in interface ThreadPool
      Throws:
      InterruptedException - if thread was interrupted
    • getThreads

      public int getThreads()
      Specified by:
      getThreads in interface ThreadPool
      Returns:
      The total number of threads currently in the pool
    • getIdleThreads

      public int getIdleThreads()
      Specified by:
      getIdleThreads in interface ThreadPool
      Returns:
      The number of idle threads in the pool
    • isLowOnThreads

      public boolean isLowOnThreads()
      Specified by:
      isLowOnThreads in interface ThreadPool
      Returns:
      True if the pool is low on threads
    • tryExecute

      public boolean tryExecute(Runnable task)
      Description copied from interface: TryExecutor
      Attempt to execute a task.
      Specified by:
      tryExecute in interface TryExecutor
      Parameters:
      task - The task to be executed
      Returns:
      True IFF the task has been given directly to a thread to execute. The task cannot be queued pending the later availability of a Thread.
    • execute

      public void execute(Runnable task)
      Specified by:
      execute in interface Executor
      Specified by:
      execute in interface TryExecutor