Class DefaultPool

java.lang.Object
org.apache.groovy.runtime.async.DefaultPool
All Implemented Interfaces:
Pool, AutoCloseable, Executor

public final class DefaultPool extends Object implements Pool
Default implementation of Pool.

Sized pools (fixed(int), cpu()) use ForkJoinPool for work-stealing and parallel stream isolation. virtual() and io() use virtual threads when that executor can be created. Every worker is created with the pool it belongs to. current() uses that when the thread has no explicit binding, so a nested call sees the pool without rebinding on each element.

Since:
6.0.0
See Also:
  • Method Details

    • virtual

      public static Pool virtual()
      Creates a virtual-thread-per-task pool, or a cached daemon pool when virtual threads cannot be created.
      Returns:
      a new virtual thread pool
    • fixed

      public static Pool fixed(int size)
      Creates a fixed-size pool backed by ForkJoinPool for work-stealing and parallel stream isolation.

      Parallelism is size and is a hard cap. Blocked workers are not replaced, so CPU-bound work cannot raise the thread count. A parallel-collection callback that blocks can stall the pool when every worker blocks with a sibling still queued; leave one spare thread, or use virtual() / io() for waiting work.

      Parameters:
      size - the number of threads (must be > 0)
      Returns:
      a new fixed pool
    • cpu

      public static Pool cpu()
      Creates a pool sized to availableProcessors(), backed by ForkJoinPool. Ideal for CPU-bound work and parallel collections. The parallelism cap matches fixed(int).
      Returns:
      a new CPU pool
    • io

      public static Pool io()
      Creates a pool for I/O-bound work. Uses virtual threads when that executor can be created, otherwise a fixed pool sized by ConcurrentConfig.getDefaultParallelism().
      Returns:
      a new I/O pool
    • current

      public static Pool current()
      Returns the pool bound to the current scope, or the pool carried by the current worker, or null. An explicit binding wins, including a binding of null. A thread that is not one of this pool's workers, including a common-pool worker, does not carry a pool.
      Returns:
      the current pool, or null if none is bound and this thread does not carry one
    • withCurrent

      public static <T> T withCurrent(Pool pool, Supplier<T> supplier)
      Executes the supplier with the given pool as current.
      Type Parameters:
      T - the result type
      Parameters:
      pool - the pool to bind
      supplier - the work to execute
      Returns:
      the supplier's result
    • execute

      public void execute(Runnable command)
      Specified by:
      execute in interface Executor
    • getPoolSize

      public int getPoolSize()
      Description copied from interface: Pool
      Returns the configured pool size. For virtual thread pools, returns Integer.MAX_VALUE.
      Specified by:
      getPoolSize in interface Pool
    • getActiveCount

      public int getActiveCount()
      Description copied from interface: Pool
      Returns the approximate number of threads actively executing tasks.
      Specified by:
      getActiveCount in interface Pool
    • usesVirtualThreads

      public boolean usesVirtualThreads()
      Description copied from interface: Pool
      Returns true if this pool uses virtual threads.
      Specified by:
      usesVirtualThreads in interface Pool
    • asForkJoinPool

      public ForkJoinPool asForkJoinPool()
      Description copied from interface: Pool
      Returns the underlying ForkJoinPool, if this pool is backed by one. Required for parallel stream isolation.

      Parallel collection methods catch UnsupportedOperationException and run on this pool as an Executor instead of the common fork-join pool. Any other exception propagates.

      Specified by:
      asForkJoinPool in interface Pool
      Returns:
      the ForkJoinPool
    • shutdown

      public void shutdown()
      Description copied from interface: Pool
      Initiates an orderly shutdown. Previously submitted tasks are executed, but no new tasks will be accepted.
      Specified by:
      shutdown in interface Pool
    • toString

      public String toString()
      Overrides:
      toString in class Object