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 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
-
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
-