|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.concurrent; |
| 20 | + |
| 21 | +import groovy.lang.Closure; |
| 22 | +import org.apache.groovy.runtime.async.AsyncSupport; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.concurrent.CompletionStage; |
| 27 | +import java.util.concurrent.Executor; |
| 28 | +import java.util.concurrent.Future; |
| 29 | + |
| 30 | +/** |
| 31 | + * User-facing API for Groovy's {@code async}/{@code await} language feature. |
| 32 | + * <p> |
| 33 | + * Provides methods for awaiting asynchronous results, creating async |
| 34 | + * computations, and configuring the async executor. Inside {@code async} |
| 35 | + * methods the {@code await expr} expression is compiled to call the internal |
| 36 | + * runtime support directly, but users may also call these methods explicitly: |
| 37 | + * <pre> |
| 38 | + * import static groovy.concurrent.AsyncUtils.* |
| 39 | + * |
| 40 | + * def future = async { |
| 41 | + * def result = await someAsyncCall() |
| 42 | + * return result * 2 |
| 43 | + * } |
| 44 | + * </pre> |
| 45 | + * <p> |
| 46 | + * <b>Thread pool configuration:</b> On JDK 21+ the default executor uses |
| 47 | + * virtual threads. On earlier JDKs a dedicated daemon thread pool is used, |
| 48 | + * whose size is controlled by the system property |
| 49 | + * {@code groovy.async.parallelism} (default: {@code availableProcessors + 1}). |
| 50 | + * The executor can be overridden at any time via {@link #setExecutor}. |
| 51 | + * |
| 52 | + * @see groovy.transform.Async |
| 53 | + * @see Awaitable |
| 54 | + * @see AwaitableAdapterRegistry |
| 55 | + * @since 6.0.0 |
| 56 | + */ |
| 57 | +public class AsyncUtils { |
| 58 | + |
| 59 | + private AsyncUtils() { } |
| 60 | + |
| 61 | + // ---- await overloads ------------------------------------------------ |
| 62 | + |
| 63 | + /** |
| 64 | + * Awaits the result of an {@link Awaitable}. |
| 65 | + * <p> |
| 66 | + * Exception handling follows the same principle as C# and JavaScript: |
| 67 | + * the <em>original</em> exception is rethrown transparently, even if it |
| 68 | + * is a checked exception. |
| 69 | + */ |
| 70 | + public static <T> T await(Awaitable<T> awaitable) { |
| 71 | + return AsyncSupport.await(awaitable); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Awaits the result of a {@link CompletableFuture}. |
| 76 | + */ |
| 77 | + public static <T> T await(CompletableFuture<T> future) { |
| 78 | + return AsyncSupport.await(future); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Awaits the result of a {@link CompletionStage}. |
| 83 | + */ |
| 84 | + public static <T> T await(CompletionStage<T> stage) { |
| 85 | + return AsyncSupport.await(stage); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Awaits the result of a {@link Future}. |
| 90 | + */ |
| 91 | + public static <T> T await(Future<T> future) { |
| 92 | + return AsyncSupport.await(future); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Awaits an arbitrary object by adapting it to {@link Awaitable} via the |
| 97 | + * {@link AwaitableAdapterRegistry}. |
| 98 | + */ |
| 99 | + public static <T> T await(Object source) { |
| 100 | + return AsyncSupport.await(source); |
| 101 | + } |
| 102 | + |
| 103 | + // ---- async ---------------------------------------------------------- |
| 104 | + |
| 105 | + /** |
| 106 | + * Executes the given closure asynchronously using the default executor, |
| 107 | + * returning an {@link Awaitable}. |
| 108 | + */ |
| 109 | + public static <T> Awaitable<T> async(Closure<T> closure) { |
| 110 | + return AsyncSupport.async(closure); |
| 111 | + } |
| 112 | + |
| 113 | + // ---- awaitAll / awaitAny / awaitAllSettled --------------------------- |
| 114 | + |
| 115 | + /** |
| 116 | + * Waits for all given awaitables and returns their results as a list. |
| 117 | + * Analogous to JavaScript's {@code Promise.all()}. |
| 118 | + */ |
| 119 | + public static List<Object> awaitAll(Object... awaitables) { |
| 120 | + return AsyncSupport.awaitAll(awaitables); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Waits for any one of the given awaitables and returns its result. |
| 125 | + * Analogous to JavaScript's {@code Promise.race()}. |
| 126 | + */ |
| 127 | + public static Object awaitAny(Object... awaitables) { |
| 128 | + return AsyncSupport.awaitAny(awaitables); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Waits for all given awaitables and returns a list of {@link AwaitResult} |
| 133 | + * objects. Analogous to JavaScript's {@code Promise.allSettled()}. |
| 134 | + */ |
| 135 | + public static List<AwaitResult<Object>> awaitAllSettled(Object... awaitables) { |
| 136 | + return AsyncSupport.awaitAllSettled(awaitables); |
| 137 | + } |
| 138 | + |
| 139 | + // ---- for-await support ---------------------------------------------- |
| 140 | + |
| 141 | + /** |
| 142 | + * Converts an arbitrary source to an {@link AsyncStream} via the adapter |
| 143 | + * registry. Useful for manual iteration with {@code for await} loops. |
| 144 | + * Returns an empty stream for {@code null} input. |
| 145 | + */ |
| 146 | + public static <T> AsyncStream<T> toAsyncStream(Object source) { |
| 147 | + return AsyncSupport.toAsyncStream(source); |
| 148 | + } |
| 149 | + |
| 150 | + // ---- async execution ------------------------------------------------ |
| 151 | + |
| 152 | + /** |
| 153 | + * Executes the given closure asynchronously on the specified executor, |
| 154 | + * returning an {@link Awaitable}. |
| 155 | + */ |
| 156 | + public static <T> Awaitable<T> executeAsync(Closure<T> closure, Executor executor) { |
| 157 | + return AsyncSupport.executeAsync(closure, executor); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Void variant of {@link #executeAsync(Closure, Executor)} for void methods. |
| 162 | + */ |
| 163 | + public static Awaitable<Void> executeAsyncVoid(Closure<?> closure, Executor executor) { |
| 164 | + return AsyncSupport.executeAsyncVoid(closure, executor); |
| 165 | + } |
| 166 | + |
| 167 | + // ---- exception utilities -------------------------------------------- |
| 168 | + |
| 169 | + /** |
| 170 | + * Deeply unwraps nested exception wrapper layers to find the original exception. |
| 171 | + */ |
| 172 | + public static Throwable deepUnwrap(Throwable t) { |
| 173 | + return AsyncSupport.deepUnwrap(t); |
| 174 | + } |
| 175 | + |
| 176 | + // ---- executor configuration ----------------------------------------- |
| 177 | + |
| 178 | + /** |
| 179 | + * Returns {@code true} if the running JVM supports virtual threads (JDK 21+). |
| 180 | + */ |
| 181 | + public static boolean isVirtualThreadsAvailable() { |
| 182 | + return AsyncSupport.isVirtualThreadsAvailable(); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Returns the current executor used for async operations. |
| 187 | + */ |
| 188 | + public static Executor getExecutor() { |
| 189 | + return AsyncSupport.getExecutor(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Sets the executor to use for async operations. Pass {@code null} |
| 194 | + * to reset to the default (virtual thread executor on JDK 21+, |
| 195 | + * or a dedicated daemon thread pool whose size is controlled by the |
| 196 | + * system property {@code groovy.async.parallelism}). |
| 197 | + */ |
| 198 | + public static void setExecutor(Executor executor) { |
| 199 | + AsyncSupport.setExecutor(executor); |
| 200 | + } |
| 201 | +} |
0 commit comments