Interface Pool
- All Superinterfaces:
AutoCloseable,Executor
- All Known Implementing Classes:
DefaultPool
Pool extends Executor so it can be used anywhere an
executor is expected — including AsyncScope.withScope(executor, body).
It also implements AutoCloseable for use in try-with-resources
blocks, ensuring clean shutdown.
Factory methods provide common pool configurations:
virtual()— virtual-thread-per-task (JDK 21+), ideal for I/Ofixed(int)— fixed-size thread pool with daemon threadscpu()— sized toavailableProcessors(), for CPU-bound workio()— larger pool for I/O-bound or blocking operations
Inspired by GPars' PGroup pool management, modernised for
virtual threads.
try (var pool = Pool.cpu()) {
AsyncScope.withScope(pool, scope -> {
scope.async(() -> cpuIntensiveWork());
scope.async(() -> moreCpuWork());
return null;
});
}
- Since:
- 6.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the underlyingForkJoinPool, if this pool is backed by one.default voidclose()Shuts down the pool.static Poolcpu()Creates a pool sized toRuntime.availableProcessors(), suitable for CPU-bound work.static Poolcurrent()Returns the pool bound to the current scope, the pool carried by the current worker thread, ornull.static Poolfixed(int size) Creates a fixed-size thread pool with daemon threads.intReturns the approximate number of threads actively executing tasks.intReturns the configured pool size.static Poolio()Creates a larger pool suitable for I/O-bound or blocking operations.voidshutdown()Initiates an orderly shutdown.booleanReturnstrueif this pool uses virtual threads.static Poolvirtual()Creates a virtual-thread-per-task pool (JDK 21+).static <T> TwithCurrent(Pool pool, Supplier<T> supplier) Executes the supplier with the given pool as current, restoring the previous binding afterwards.
-
Method Details
-
virtual
Creates a virtual-thread-per-task pool (JDK 21+).Each submitted task runs on its own virtual thread. This is ideal for I/O-bound workloads where tasks spend time waiting. On JDK versions that do not support virtual threads, falls back to a cached daemon thread pool.
Parallel collection methods submit one task per element to this pool. They do not fall back to
ForkJoinPool.commonPool().- Returns:
- a new virtual thread pool
-
fixed
Creates a fixed-size thread pool with daemon threads.The pool is a
ForkJoinPoolwhose parallelism issize. That parallelism is a hard cap: blocked workers are not replaced. Parallel collection methods run at this cap. A blocking callback can stall the pool when every worker blocks with a sibling still queued; leave one spare thread, or usevirtual()/io()for waiting work.- Parameters:
size- the number of threads (must be > 0)- Returns:
- a new fixed pool
- Throws:
IllegalArgumentException- if size ≤ 0
-
cpu
Creates a pool sized toRuntime.availableProcessors(), suitable for CPU-bound work.- Returns:
- a new CPU pool
-
io
Creates a larger pool suitable for I/O-bound or blocking operations.If virtual threads are available, returns a virtual thread pool (the ideal choice for I/O) and parallel collection methods run on it. Otherwise, including when the virtual-thread factory exists but cannot create an executor, returns
fixed(int)sized toConcurrentConfig.getDefaultParallelism(), which has that pool's parallelism cap.- Returns:
- a new I/O pool
-
current
Returns the pool bound to the current scope, the pool carried by the current worker thread, ornull.Set by
ParallelScope.withPool(int, java.util.function.Function<groovy.concurrent.AsyncScope, T>)using the runtime's scoped binding support. An explicitwithCurrent(groovy.concurrent.Pool, java.util.function.Supplier<T>)binding wins, including a binding ofnull. Otherwise a worker created for a pool reports that pool. A common-pool thread does not.- Returns:
- the current pool, or
nullif none is bound and this thread does not carry one - Since:
- 6.0.0
-
withCurrent
Executes the supplier with the given pool as current, restoring the previous binding afterwards.- Type Parameters:
T- the result type- Parameters:
pool- the pool to bindsupplier- the work to execute- Returns:
- the supplier's result
- Since:
- 6.0.0
-
getPoolSize
int getPoolSize()Returns the configured pool size. For virtual thread pools, returnsInteger.MAX_VALUE. -
getActiveCount
int getActiveCount()Returns the approximate number of threads actively executing tasks. -
usesVirtualThreads
boolean usesVirtualThreads()Returnstrueif this pool uses virtual threads. -
asForkJoinPool
ForkJoinPool asForkJoinPool()Returns the underlyingForkJoinPool, if this pool is backed by one. Required for parallel stream isolation.Parallel collection methods catch
UnsupportedOperationExceptionand run on this pool as anExecutorinstead of the common fork-join pool. Any other exception propagates.- Returns:
- the ForkJoinPool
- Throws:
UnsupportedOperationException- if this pool is not ForkJoinPool-backed (e.g., virtual thread pools)- Since:
- 6.0.0
-
shutdown
void shutdown()Initiates an orderly shutdown. Previously submitted tasks are executed, but no new tasks will be accepted. -
close
default void close()Shuts down the pool. Equivalent toshutdown().- Specified by:
closein interfaceAutoCloseable
-