Package org.apache.groovy.runtime.async
Class DefaultAsyncScope
java.lang.Object
org.apache.groovy.runtime.async.DefaultAsyncScope
- All Implemented Interfaces:
AsyncScope,AutoCloseable
Default implementation of
AsyncScope providing structured
concurrency with configurable failure policy.
A dedicated lock guards the child task list and the closed flag jointly,
ensuring that async(Supplier) and close() cannot race.
Child futures are registered under the lock before task submission
(register-before-submit protocol), guaranteeing every child is joined or
cancelled by close().
- Since:
- 6.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultAsyncScope(Executor executor) DefaultAsyncScope(Executor executor, boolean failFast) -
Method Summary
Modifier and TypeMethodDescription<T> Awaitable<T>Launches a child task within this scope.voidCancels all child tasks.voidclose()Closes the scope, waiting for all child tasks to complete.static AsyncScopecurrent()Returns the current scope, ornullwhen no scope is bound.intReturns the number of tracked child tasks (including completed ones that have not yet been pruned).Returns the parent scope, ornullif this is a root scope.toString()static <T> TwithCurrent(AsyncScope scope, Supplier<T> supplier) Executes the supplier with the given scope as current, restoring the previous binding afterwards.static <T> TwithScopeTimeout(Executor executor, Duration timeout, Function<AsyncScope, T> body) Creates a scope with a timeout.
-
Constructor Details
-
DefaultAsyncScope
-
DefaultAsyncScope
-
DefaultAsyncScope
public DefaultAsyncScope()
-
-
Method Details
-
current
Returns the current scope, ornullwhen no scope is bound. -
withCurrent
Executes the supplier with the given scope as current, restoring the previous binding afterwards. -
getParent
Description copied from interface:AsyncScopeReturns the parent scope, ornullif this is a root scope.When a scope is created inside another scope (via
AsyncScope.withScope(java.util.function.Function<groovy.concurrent.AsyncScope, T>)), the outer scope becomes the parent. Cancelling a parent scope propagates cancellation to all child scopes.- Specified by:
getParentin interfaceAsyncScope- Returns:
- the parent scope, or
null
-
async
Description copied from interface:AsyncScopeLaunches a child task within this scope. The task's lifetime is bound to the scope: when the scope is closed, all incomplete child tasks are cancelled.- Specified by:
asyncin interfaceAsyncScope- Type Parameters:
T- the result type- Parameters:
supplier- the task body to execute- Returns:
- an
Awaitablerepresenting the child task
-
getChildCount
public int getChildCount()Description copied from interface:AsyncScopeReturns the number of tracked child tasks (including completed ones that have not yet been pruned).- Specified by:
getChildCountin interfaceAsyncScope
-
cancelAll
public void cancelAll()Description copied from interface:AsyncScopeCancels all child tasks.- Specified by:
cancelAllin interfaceAsyncScope
-
close
public void close()Description copied from interface:AsyncScopeCloses the scope, waiting for all child tasks to complete. If any child failed and fail-fast is enabled, remaining children are cancelled and the first failure is rethrown.- Specified by:
closein interfaceAsyncScope- Specified by:
closein interfaceAutoCloseable
-
withScopeTimeout
public static <T> T withScopeTimeout(Executor executor, Duration timeout, Function<AsyncScope, T> body) throws TimeoutExceptionCreates a scope with a timeout. If the body does not complete within the duration, all children are cancelled andTimeoutExceptionis thrown.- Throws:
TimeoutException
-
toString
-