Internal runtime support for the async/await/defer language features. This class contains the actual implementation invoked by compiler-generated code. User code should prefer the static methods on groovy.concurrent.Awaitable for combinators and configuration.
| 81 | * @since 6.0.0 |
| 82 | */ |
| 83 | public class AsyncSupport { |
| 84 | |
| 85 | private static final boolean VIRTUAL_THREADS_AVAILABLE; |
| 86 | private static final Executor VIRTUAL_THREAD_EXECUTOR; |
| 87 | private static final int FALLBACK_MAX_THREADS; |
| 88 | private static final Executor FALLBACK_EXECUTOR; |
| 89 | |
| 90 | static { |
| 91 | Executor vtExecutor = null; |
| 92 | boolean vtAvailable = false; |
| 93 | try { |
| 94 | MethodHandle mh = MethodHandles.lookup().findStatic( |
| 95 | Executors.class, "newVirtualThreadPerTaskExecutor", |
| 96 | MethodType.methodType(ExecutorService.class)); |
| 97 | vtExecutor = (Executor) mh.invoke(); |
| 98 | vtAvailable = true; |
| 99 | } catch (Throwable ignored) { |
| 100 | // JDK < 21 — virtual threads not available |
| 101 | } |
| 102 | VIRTUAL_THREAD_EXECUTOR = vtExecutor; |
| 103 | VIRTUAL_THREADS_AVAILABLE = vtAvailable; |
| 104 | |
| 105 | FALLBACK_MAX_THREADS = getIntegerSafe("groovy.async.parallelism", 256); |
| 106 | if (!VIRTUAL_THREADS_AVAILABLE) { |
| 107 | FALLBACK_EXECUTOR = new ThreadPoolExecutor( |
| 108 | 0, FALLBACK_MAX_THREADS, |
| 109 | 60L, TimeUnit.SECONDS, |
| 110 | new SynchronousQueue<>(), |
| 111 | r -> { |
| 112 | Thread t = new Thread(r); |
| 113 | t.setDaemon(true); |
| 114 | @SuppressWarnings("deprecation") |
| 115 | long id = t.getId(); |
| 116 | t.setName("groovy-async-" + id); |
| 117 | return t; |
| 118 | }, |
| 119 | new ThreadPoolExecutor.CallerRunsPolicy()); |
| 120 | } else { |
| 121 | FALLBACK_EXECUTOR = null; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private static volatile Executor defaultExecutor = createDefaultExecutor(); |
| 126 | |
| 127 | private static final ScheduledExecutorService SCHEDULER = |
| 128 | Executors.newSingleThreadScheduledExecutor(r -> { |
| 129 | Thread t = new Thread(r, "groovy-async-scheduler"); |
| 130 | t.setDaemon(true); |
| 131 | return t; |
| 132 | }); |
| 133 | |
| 134 | private AsyncSupport() { } |
| 135 | |
| 136 | // ---- executor configuration ----------------------------------------- |
| 137 | |
| 138 | /** Returns the shared scheduler for delays, timeouts, and scope deadlines. */ |
| 139 | public static ScheduledExecutorService getScheduler() { |
| 140 | return SCHEDULER; |
nothing calls this directly
no test coverage detected
searching dependent graphs…