Package org.apache.groovy.runtime.async

# 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. I/O pools (`virtual()`, `io()`) use virtual threads on JDK 21+.

Since:
  6.0.0
See Also:
  - `Pool`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ForkJoinPool`
  `asForkJoinPool()`
  Returns the underlying `ForkJoinPool`, if this pool is backed by one.
  `static Pool`
  `cpu()`
  Creates a pool sized to `availableProcessors()`, backed by `ForkJoinPool`.
  `static Pool`
  `current()`
  Returns the pool bound to the current scope, or `null`.
  `void`
  `execute(Runnable command)`
  `static Pool`
  `fixed(int size)`
  Creates a fixed-size pool backed by `ForkJoinPool` for work-stealing and parallel stream isolation.
  `int`
  `getActiveCount()`
  Returns the approximate number of threads actively executing tasks.
  `int`
  `getPoolSize()`
  Returns the configured pool size.
  `static Pool`
  `io()`
  Creates a pool for I/O-bound work.
  `void`
  `shutdown()`
  Initiates an orderly shutdown.
  `String`
  `toString()`
  `boolean`
  `usesVirtualThreads()`
  Returns `true` if this pool uses virtual threads.
  `static Pool`
  `virtual()`
  Creates a virtual-thread-per-task pool, or a cached daemon pool as fallback on JDK < 21.
  `static <T> T`
  `withCurrent(Pool pool, Supplier<T> supplier)`
  Executes the supplier with the given pool as current.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface groovy.concurrent.Pool

  `close`

- ## Method Details

  - ### virtual

    public static Pool virtual()
    Creates a virtual-thread-per-task pool, or a cached daemon pool as fallback on JDK < 21.
    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.
    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.
    Returns:
      a new CPU pool

  - ### io

    public static Pool io()
    Creates a pool for I/O-bound work. Uses virtual threads if available, 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 `null`.
    Returns:
      the current pool, or `null` if none is bound

  - ### 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.
    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`