Package org.apache.groovy.runtime.async

# Class DefaultAsyncScope

java.lang.Object
org.apache.groovy.runtime.async.DefaultAsyncScope

All Implemented Interfaces:
  `AsyncScope`, `AutoCloseable`

public final class DefaultAsyncScope extends Object implements AsyncScope
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:
  - `AsyncScope`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultAsyncScope()`
  `DefaultAsyncScope(Executor executor)`
  `DefaultAsyncScope(Executor executor, boolean failFast)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `<T> Awaitable<T>`
  `async(Supplier<T> supplier)`
  Launches a child task within this scope.
  `void`
  `cancelAll()`
  Cancels all child tasks.
  `void`
  `close()`
  Closes the scope, waiting for all child tasks to complete.
  `static AsyncScope`
  `current()`
  Returns the current scope, or `null` when no scope is bound.
  `int`
  `getChildCount()`
  Returns the number of tracked child tasks (including completed ones that have not yet been pruned).
  `AsyncScope`
  `getParent()`
  Returns the parent scope, or `null` if this is a root scope.
  `String`
  `toString()`
  `static <T> T`
  `withCurrent(AsyncScope scope, Supplier<T> supplier)`
  Executes the supplier with the given scope as current, restoring the previous binding afterwards.
  `static <T> T`
  `withScopeTimeout(Executor executor, Duration timeout, Function<AsyncScope,T> body)`
  Creates a scope with a timeout.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### DefaultAsyncScope

    public DefaultAsyncScope(Executor executor, boolean failFast)

  - ### DefaultAsyncScope

    public DefaultAsyncScope(Executor executor)

  - ### DefaultAsyncScope

    public DefaultAsyncScope()

- ## Method Details

  - ### current

    public static AsyncScope current()
    Returns the current scope, or `null` when no scope is bound.

  - ### withCurrent

    public static <T> T withCurrent(AsyncScope scope, Supplier<T> supplier)
    Executes the supplier with the given scope as current, restoring the previous binding afterwards.

  - ### getParent

    public AsyncScope getParent()
    Description copied from interface: `AsyncScope`
    Returns the parent scope, or `null` if 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:
      `getParent` in interface `AsyncScope`
    Returns:
      the parent scope, or `null`

  - ### async

    public <T> Awaitable<T> async(Supplier<T> supplier)
    Description copied from interface: `AsyncScope`
    Launches 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:
      `async` in interface `AsyncScope`
    Type Parameters:
      `T` - the result type
    Parameters:
      `supplier` - the task body to execute
    Returns:
      an `Awaitable` representing the child task

  - ### getChildCount

    public int getChildCount()
    Description copied from interface: `AsyncScope`
    Returns the number of tracked child tasks (including completed ones that have not yet been pruned).
    Specified by:
      `getChildCount` in interface `AsyncScope`

  - ### cancelAll

    public void cancelAll()
    Description copied from interface: `AsyncScope`
    Cancels all child tasks.
    Specified by:
      `cancelAll` in interface `AsyncScope`

  - ### close

    public void close()
    Description copied from interface: `AsyncScope`
    Closes 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:
      `close` in interface `AsyncScope`
    Specified by:
      `close` in interface `AutoCloseable`

  - ### withScopeTimeout

    public static <T> T withScopeTimeout(Executor executor, Duration timeout, Function<AsyncScope,T> body) throws TimeoutException
    Creates a scope with a timeout. If the body does not complete within the duration, all children are cancelled and `TimeoutException` is thrown.
    Throws:
      `TimeoutException`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`