Class OpenStructured
java.lang.Object
cloud.opencode.base.parallel.OpenStructured
Open Structured - Structured Concurrency Facade (JDK 25 JEP 499)
Open 结构化 - 结构化并发门面 (JDK 25 JEP 499)
Provides static methods for structured concurrency operations. Structured concurrency ensures that child tasks are bound to parent task lifecycle - they cannot outlive the parent.
为结构化并发操作提供静态方法。结构化并发确保子任务绑定到父任务 的生命周期 - 它们不能超出父任务的生存期。
Key Features | 主要特性:
- All subtasks complete before scope closes - 所有子任务在作用域关闭前完成
- Automatic cancellation on parent cancellation - 父任务取消时自动取消子任务
- Exception propagation follows structured rules - 异常传播遵循结构化规则
Example | 示例:
// All must succeed
List<String> results = OpenStructured.invokeAll(List.of(
() -> fetchA(),
() -> fetchB(),
() -> fetchC()
));
// First success wins
String result = OpenStructured.invokeAny(List.of(
() -> fetchFromPrimary(),
() -> fetchFromBackup()
));
// Parallel combine
Result result = OpenStructured.parallel(
() -> fetchUser(),
() -> fetchOrders(),
(user, orders) -> new Result(user, orders)
);
Security | 安全性:
- Thread-safe: Yes (utility class, stateless) - 线程安全: 是(工具类,无状态)
- Since:
- JDK 25, opencode-base-parallel V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> Invokes all tasks, failing fast if any task fails.static <T> List<T> Invokes all tasks with timeout.static <T> TInvokes all tasks, returning first success.static <T> TInvokes all tasks with timeout, returning first success.static <T,U, V, R> R parallel(Callable<T> task1, Callable<U> task2, Callable<V> task3, TriFunction<T, U, V, R> combiner) Invokes three tasks in parallel and combines results.static <T,U, R> R parallel(Callable<T> task1, Callable<U> task2, BiFunction<T, U, R> combiner) Invokes two tasks in parallel and combines results.static <T> TRaces multiple tasks, returning the first to complete.static <T,R> List <R> runAllWithContext(ScopedValue<T> scopedValue, T value, List<Callable<R>> tasks) Runs multiple tasks with a scoped value bound.static <T,R> R runWithContext(ScopedValue<T> scopedValue, T value, Callable<R> task) Runs a task with a scoped value bound.static <T> StructuredScope<T> scope()Creates a structured scope with shutdown-on-failure policy.static <T> StructuredScope<T> scopeAny()Creates a structured scope with shutdown-on-success policy.
-
Method Details
-
invokeAll
-
invokeAll
-
parallel
Invokes two tasks in parallel and combines results. 并行调用两个任务并组合结果。- Type Parameters:
T- the first type - 第一个类型U- the second type - 第二个类型R- the result type - 结果类型- Parameters:
task1- the first task - 第一个任务task2- the second task - 第二个任务combiner- the combiner function - 组合函数- Returns:
- the combined result - 组合结果
-
parallel
public static <T,U, R parallelV, R> (Callable<T> task1, Callable<U> task2, Callable<V> task3, TriFunction<T, U, V, R> combiner) Invokes three tasks in parallel and combines results. 并行调用三个任务并组合结果。- Type Parameters:
T- the first type - 第一个类型U- the second type - 第二个类型V- the third type - 第三个类型R- the result type - 结果类型- Parameters:
task1- the first task - 第一个任务task2- the second task - 第二个任务task3- the third task - 第三个任务combiner- the combiner function - 组合函数- Returns:
- the combined result - 组合结果
-
invokeAny
-
invokeAny
-
race
Races multiple tasks, returning the first to complete. 竞争多个任务,返回首个完成的。- Type Parameters:
T- the result type - 结果类型- Parameters:
tasks- the tasks - 任务- Returns:
- the first result - 首个结果
-
runWithContext
Runs a task with a scoped value bound. 使用绑定的作用域值运行任务。- Type Parameters:
T- the value type - 值类型R- the result type - 结果类型- Parameters:
scopedValue- the scoped value - 作用域值value- the value to bind - 要绑定的值task- the task - 任务- Returns:
- the result - 结果
-
runAllWithContext
public static <T,R> List<R> runAllWithContext(ScopedValue<T> scopedValue, T value, List<Callable<R>> tasks) Runs multiple tasks with a scoped value bound. 使用绑定的作用域值运行多个任务。- Type Parameters:
T- the value type - 值类型R- the result type - 结果类型- Parameters:
scopedValue- the scoped value - 作用域值value- the value to bind - 要绑定的值tasks- the tasks - 任务- Returns:
- the results - 结果
-
scope
Creates a structured scope with shutdown-on-failure policy. 创建具有失败关闭策略的结构化作用域。- Type Parameters:
T- the result type - 结果类型- Returns:
- the scope - 作用域
-
scopeAny
Creates a structured scope with shutdown-on-success policy. 创建具有成功关闭策略的结构化作用域。- Type Parameters:
T- the result type - 结果类型- Returns:
- the scope - 作用域
-