Class AsyncCompletionScope
Completion callbacks sometimes need to start asynchronous side effects, such as aggregate commits, without blocking each callback immediately. This scope lets those callbacks register their futures and waits for all registered work after the callback group has finished running.
The scope is intentionally thread-local. Work registered from other threads is only included when that thread is executing inside the same logical scope.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisActive()Returns whether the current thread is executing inside an async completion scope.static <T> CompletableFuture<T> register(CompletableFuture<T> future) Registers a future with the current completion scope.static <T> CompletableFuture<T> register(CompletableFuture<T> future, Runnable afterCompletion) Registers a future with the current completion scope and runs a callback after scoped futures have completed.static voidrunAndAwait(Runnable task) Runs the supplied task inside a new completion scope and waits for all futures registered in that scope.
-
Method Details
-
runAndAwait
Runs the supplied task inside a new completion scope and waits for all futures registered in that scope.If both the task and asynchronous completion fail, the asynchronous failure is added as a suppressed exception to the task failure.
- Parameters:
task- the task that may register asynchronous completion work
-
register
Registers a future with the current completion scope.When no scope is active, the future is returned unchanged and no waiting behavior is added. This makes callers free to register futures unconditionally while preserving the normal behavior outside handler or batch completion.
- Type Parameters:
T- result type of the future- Parameters:
future- future to await at the end of the active scope;nullis treated as already completed- Returns:
- the supplied future, or a completed future when
futureisnull
-
register
public static <T> CompletableFuture<T> register(CompletableFuture<T> future, Runnable afterCompletion) Registers a future with the current completion scope and runs a callback after scoped futures have completed.The callback runs on the thread that owns the completion scope, after all registered futures have completed and before any asynchronous failure is rethrown from
runAndAwait(Runnable). This is useful for cleaning up thread-local state that must remain visible until asynchronous completion work has finished.When no scope is active, the future is returned unchanged and the callback is not invoked.
- Type Parameters:
T- result type of the future- Parameters:
future- future to await at the end of the active scope;nullis treated as already completedafterCompletion- callback to run after scoped futures have completed; may benull- Returns:
- the supplied future, or a completed future when
futureisnull
-
isActive
public static boolean isActive()Returns whether the current thread is executing inside an async completion scope.- Returns:
truewhen futures registered by this thread will be awaited by a surrounding scope
-