Class DefaultPool
- All Implemented Interfaces:
Pool,AutoCloseable,Executor
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 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, or the pool carried by the current worker, 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 when virtual threads cannot be created.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 when virtual threads cannot be created.- Returns:
- a new virtual thread pool
-
fixed
Creates a fixed-size pool backed byForkJoinPoolfor work-stealing and parallel stream isolation.Parallelism is
sizeand 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 usevirtual()/io()for waiting work.- 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. The parallelism cap matchesfixed(int).- Returns:
- a new CPU pool
-
io
Creates a pool for I/O-bound work. Uses virtual threads when that executor can be created, otherwise a fixed pool sized byConcurrentConfig.getDefaultParallelism().- Returns:
- a new I/O pool
-
current
Returns the pool bound to the current scope, or the pool carried by the current worker, ornull. An explicit binding wins, including a binding ofnull. 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
nullif none is bound and this thread does not carry one
-
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.Parallel collection methods catch
UnsupportedOperationExceptionand run on this pool as anExecutorinstead of the common fork-join pool. Any other exception propagates.- 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
-