MCPcopy Index your code
hub / github.com/apache/groovy / DefaultPool

Class DefaultPool

src/main/java/org/apache/groovy/runtime/async/DefaultPool.java:44–191  ·  view source on GitHub ↗

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+. @see Pool @since 6.0.0

Source from the content-addressed store, hash-verified

42 * @since 6.0.0
43 */
44public final class DefaultPool implements Pool {
45
46 // ---- Virtual thread detection ----------------------------------------
47
48 private static final MethodHandle NEW_VT_EXECUTOR;
49 private static final MethodType NEW_VT_EXECUTOR_TYPE = MethodType.methodType(ExecutorService.class);
50
51 static {
52 MethodHandle mh = null;
53 try {
54 mh = MethodHandles.lookup().findStatic(
55 Executors.class, "newVirtualThreadPerTaskExecutor",
56 NEW_VT_EXECUTOR_TYPE)
57 .asType(NEW_VT_EXECUTOR_TYPE);
58 } catch (Throwable ignored) { }
59 NEW_VT_EXECUTOR = mh;
60 }
61
62 private static final ScopedLocal<Pool> CURRENT_POOL = ScopedLocal.newInstance();
63
64 // ---- Instance fields -------------------------------------------------
65
66 private final ExecutorService executor;
67 private final int poolSize;
68 private final boolean virtualThreads;
69
70 private DefaultPool(ExecutorService executor, int poolSize, boolean virtualThreads) {
71 this.executor = executor;
72 this.poolSize = poolSize;
73 this.virtualThreads = virtualThreads;
74 }
75
76 // ---- Factory methods ------------------------------------------------
77
78 /**
79 * Creates a virtual-thread-per-task pool, or a cached daemon pool
80 * as fallback on JDK &lt; 21.
81 */
82 public static Pool virtual() {
83 if (NEW_VT_EXECUTOR != null) {
84 try {
85 ExecutorService es = (ExecutorService) NEW_VT_EXECUTOR.invokeExact();
86 return new DefaultPool(es, Integer.MAX_VALUE, true);
87 } catch (Throwable ignored) { }
88 }
89 AtomicLong counter = new AtomicLong();
90 ExecutorService es = Executors.newCachedThreadPool(r -> {
91 Thread t = new Thread(r, "groovy-pool-virtual-" + counter.incrementAndGet());
92 t.setDaemon(true);
93 return t;
94 });
95 return new DefaultPool(es, Integer.MAX_VALUE, false);
96 }
97
98 /**
99 * Creates a fixed-size pool backed by {@link ForkJoinPool} for
100 * work-stealing and parallel stream isolation.
101 */

Callers

nothing calls this directly

Calls 3

newInstanceMethod · 0.95
asTypeMethod · 0.45
lookupMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…