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(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 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. Uses ScopedValue on JDK 25+, ThreadLocal on earlier JDKs.
    • withCurrent

      public static <T> T withCurrent(AsyncScope scope, Supplier<T> supplier)
      Executes the supplier with the given scope as current. Uses ScopedValue.where().call() on JDK 25+ for optimal virtual thread performance; falls back to ThreadLocal set/restore on earlier JDKs.
    • async

      public <T> Awaitable<T> async(Closure<T> body)
      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:
      body - the async body to execute
      Returns:
      an Awaitable representing the child task
    • async

      public <T> Awaitable<T> async(Supplier<T> supplier)
      Description copied from interface: AsyncScope
      Launches a child task using a Supplier for Java interop.
      Specified by:
      async in interface AsyncScope
    • 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
    • toString

      public String toString()
      Overrides:
      toString in class Object