Package org.apache.groovy.runtime.async
Class DefaultPool
java.lang.Object
org.apache.groovy.runtime.async.DefaultPool
- All Implemented Interfaces:
Pool,AutoCloseable,Executor
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:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the underlyingForkJoinPool, if this pool is backed by one.static Poolcpu()Creates a pool sized toavailableProcessors(), backed byForkJoinPool.static Poolcurrent()Returns the pool bound to the current scope, ornull.voidstatic Poolfixed(int size) Creates a fixed-size pool backed byForkJoinPoolfor work-stealing and parallel stream isolation.intReturns the approximate number of threads actively executing tasks.intReturns the configured pool size.static Poolio()Creates a pool for I/O-bound work.voidshutdown()Initiates an orderly shutdown.toString()booleanReturnstrueif this pool uses virtual threads.static Poolvirtual()Creates a virtual-thread-per-task pool, or a cached daemon pool as fallback on JDK < 21.static <T> TwithCurrent(Pool pool, Supplier<T> supplier) Executes the supplier with the given pool as current.
-
Method Details
-
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
Creates a fixed-size pool backed byForkJoinPoolfor work-stealing and parallel stream isolation.- Parameters:
size- the number of threads (must be > 0)- Returns:
- a new fixed pool
-
cpu
Creates a pool sized toavailableProcessors(), backed byForkJoinPool. Ideal for CPU-bound work and parallel collections.- Returns:
- a new CPU pool
-
io
Creates a pool for I/O-bound work. Uses virtual threads if available, otherwise a fixed pool sized byConcurrentConfig.getDefaultParallelism().- Returns:
- a new I/O pool
-
current
Returns the pool bound to the current scope, ornull.- Returns:
- the current pool, or
nullif none is bound
-
withCurrent
Executes the supplier with the given pool as current.- Type Parameters:
T- the result type- Parameters:
pool- the pool to bindsupplier- the work to execute- Returns:
- the supplier's result
-
execute
-
getPoolSize
public int getPoolSize()Description copied from interface:PoolReturns the configured pool size. For virtual thread pools, returnsInteger.MAX_VALUE.- Specified by:
getPoolSizein interfacePool
-
getActiveCount
public int getActiveCount()Description copied from interface:PoolReturns the approximate number of threads actively executing tasks.- Specified by:
getActiveCountin interfacePool
-
usesVirtualThreads
public boolean usesVirtualThreads()Description copied from interface:PoolReturnstrueif this pool uses virtual threads.- Specified by:
usesVirtualThreadsin interfacePool
-
asForkJoinPool
Description copied from interface:PoolReturns the underlyingForkJoinPool, if this pool is backed by one. Required for parallel stream isolation.- Specified by:
asForkJoinPoolin interfacePool- Returns:
- the ForkJoinPool
-
shutdown
public void shutdown()Description copied from interface:PoolInitiates an orderly shutdown. Previously submitted tasks are executed, but no new tasks will be accepted. -
toString
-