Class OpenStructured

java.lang.Object
cloud.opencode.base.parallel.OpenStructured

public final class OpenStructured extends Object
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 Details

    • invokeAll

      public static <T> List<T> invokeAll(List<Callable<T>> tasks)
      Invokes all tasks, failing fast if any task fails. 调用所有任务,任一任务失败则快速失败。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      Returns:
      the results - 结果
    • invokeAll

      public static <T> List<T> invokeAll(List<Callable<T>> tasks, Duration timeout)
      Invokes all tasks with timeout. 带超时调用所有任务。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      timeout - the timeout - 超时
      Returns:
      the results - 结果
    • parallel

      public 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. 并行调用两个任务并组合结果。
      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,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. 并行调用三个任务并组合结果。
      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

      public static <T> T invokeAny(List<Callable<T>> tasks)
      Invokes all tasks, returning first success. 调用所有任务,返回首个成功。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      Returns:
      the first successful result - 首个成功结果
    • invokeAny

      public static <T> T invokeAny(List<Callable<T>> tasks, Duration timeout)
      Invokes all tasks with timeout, returning first success. 带超时调用所有任务,返回首个成功。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      timeout - the timeout - 超时
      Returns:
      the first successful result - 首个成功结果
    • race

      @SafeVarargs public static <T> T race(Callable<T>... tasks)
      Races multiple tasks, returning the first to complete. 竞争多个任务,返回首个完成的。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      Returns:
      the first result - 首个结果
    • runWithContext

      public static <T,R> R runWithContext(ScopedValue<T> scopedValue, T value, Callable<R> task)
      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

      public static <T> StructuredScope<T> scope()
      Creates a structured scope with shutdown-on-failure policy. 创建具有失败关闭策略的结构化作用域。
      Type Parameters:
      T - the result type - 结果类型
      Returns:
      the scope - 作用域
    • scopeAny

      public static <T> StructuredScope<T> scopeAny()
      Creates a structured scope with shutdown-on-success policy. 创建具有成功关闭策略的结构化作用域。
      Type Parameters:
      T - the result type - 结果类型
      Returns:
      the scope - 作用域