Class StructuredScope<T>
java.lang.Object
cloud.opencode.base.parallel.structured.StructuredScope<T>
- Type Parameters:
T- the result type - 结果类型
- All Implemented Interfaces:
AutoCloseable
Structured Scope - Structured Concurrency Wrapper (JDK 25 JEP 499)
结构化作用域 - 结构化并发包装器 (JDK 25 JEP 499)
Provides a fluent API for structured concurrency with automatic lifecycle management of child tasks.
为结构化并发提供流式 API,自动管理子任务的生命周期。
Features | 特性:
- Child tasks bound to parent scope - 子任务绑定到父作用域
- Automatic cancellation on failure - 失败时自动取消
- Timeout support - 超时支持
- Result aggregation - 结果聚合
Example | 示例:
List<String> results = StructuredScope.shutdownOnFailure()
.fork(() -> fetchA())
.fork(() -> fetchB())
.fork(() -> fetchC())
.joinAll();
Security | 安全性:
- Thread-safe: No (use within single scope/thread) - 线程安全: 否(在单个作用域/线程内使用)
- Since:
- JDK 25, opencode-base-parallel V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Forks a task in this scope.Forks multiple tasks in this scope.final StructuredScope<T> Forks multiple tasks in this scope.Gets the policy.intGets the number of forked tasks.joinAll()Joins and returns all results.Joins with timeout and returns all results.joinAndReduce(T identity, BiFunction<T, T, T> reducer) Joins and reduces results.joinAny()Joins and returns the first successful result (for SHUTDOWN_ON_SUCCESS).List<TaskResult<T>> Joins and returns results as TaskResults.static <T> StructuredScope<T> Creates a scope that shuts down on first failure.static <T> StructuredScope<T> Creates a scope that shuts down on first success.
-
Method Details
-
shutdownOnFailure
Creates a scope that shuts down on first failure. 创建首次失败时关闭的作用域。- Type Parameters:
T- the result type - 结果类型- Returns:
- the scope - 作用域
-
shutdownOnSuccess
Creates a scope that shuts down on first success. 创建首次成功时关闭的作用域。- Type Parameters:
T- the result type - 结果类型- Returns:
- the scope - 作用域
-
fork
Forks a task in this scope. 在此作用域中分叉任务。- Parameters:
task- the task - 任务- Returns:
- this scope - 此作用域
-
forkAll
Forks multiple tasks in this scope. 在此作用域中分叉多个任务。- Parameters:
tasks- the tasks - 任务- Returns:
- this scope - 此作用域
-
forkAll
Forks multiple tasks in this scope. 在此作用域中分叉多个任务。- Parameters:
tasks- the tasks - 任务- Returns:
- this scope - 此作用域
-
joinAll
-
joinAll
Joins with timeout and returns all results. Note: In JDK 25, timeout must be configured at scope creation time via Configuration. This method provides a best-effort timeout using a virtual thread monitor. 带超时等待并返回所有结果。 注意:JDK 25 中超时须在创建时通过 Configuration 配置。 此方法通过虚拟线程监控提供尽力超时。- Parameters:
timeout- the timeout - 超时- Returns:
- the results - 结果
-
joinAny
Joins and returns the first successful result (for SHUTDOWN_ON_SUCCESS). 等待并返回首个成功结果(用于 SHUTDOWN_ON_SUCCESS)。- Returns:
- the first result - 首个结果
-
joinAndReduce
-
joinAsResults
Joins and returns results as TaskResults. 等待并返回结果为 TaskResult。- Returns:
- the task results - 任务结果
-
getTaskCount
public int getTaskCount()Gets the number of forked tasks. 获取分叉的任务数。- Returns:
- the task count - 任务数
-
getPolicy
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-