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(Closure) 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.<T> Awaitable<T>Launches a child task using aSupplierfor Java interop.voidCancels all child tasks.voidclose()Closes the scope, waiting for all child tasks to complete.static AsyncScopecurrent()Returns the current scope.intReturns the number of tracked child tasks (including completed ones that have not yet been pruned).toString()static <T> TwithCurrent(AsyncScope scope, Supplier<T> supplier) Executes the supplier with the given scope as current.
-
Constructor Details
-
DefaultAsyncScope
-
DefaultAsyncScope
-
DefaultAsyncScope
public DefaultAsyncScope()
-
-
Method Details
-
current
Returns the current scope. UsesScopedValueon JDK 25+,ThreadLocalon earlier JDKs. -
withCurrent
Executes the supplier with the given scope as current. UsesScopedValue.where().call()on JDK 25+ for optimal virtual thread performance; falls back toThreadLocalset/restore on earlier JDKs. -
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:
body- the async body to execute- Returns:
- an
Awaitablerepresenting the child task
-
async
Description copied from interface:AsyncScopeLaunches a child task using aSupplierfor Java interop.- Specified by:
asyncin interfaceAsyncScope
-
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
-
toString
-