Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B38FGKKam5SCGgwgduVAh3
28 lines
1.6 KiB
Markdown
28 lines
1.6 KiB
Markdown
# 7. Structured concurrency (JEP 533, seventh preview)
|
|
|
|
Prev: [6. Lazy constants](06-lazy-constants.md) · Next: [8. PEM](08-pem-api.md)
|
|
|
|
Compile and run with `--enable-preview --release 27`. Source: [`StructuredDemo`](../jep-tour/src/StructuredDemo.java), transcript [21](output/21-structured.txt).
|
|
|
|
The 27 API, as exercised:
|
|
|
|
```java
|
|
try (var scope = StructuredTaskScope.open(Joiner.<String>allSuccessfulOrThrow())) {
|
|
scope.fork(StructuredDemo::fetchPrice);
|
|
scope.fork(StructuredDemo::fetchStock);
|
|
List<String> results = scope.join(); // [price=42, stock=7], in fork order
|
|
}
|
|
```
|
|
|
|
* A failing subtask makes `join()` throw `ExecutionException` with the original as its cause.
|
|
* **New in 27:** `StructuredTaskScope` and `Joiner` gained a third type parameter, the exception `join()` throws. `Joiner.<String, OrderFailed>allSuccessfulOrThrow(OrderFailed::new)` makes `join()` throw *your* type.
|
|
* A timeout is a scope option (`cfg -> cfg.withTimeout(Duration.ofMillis(100))`) and surfaces as `CancelledByTimeoutException`.
|
|
|
|
## What broke from 26
|
|
|
|
[`StructuredScope26.java`](../jep-tour/src/broken/StructuredScope26.java) compiles on 26 and fails on 27 with three errors ([20-broken-structuredscope26.txt](output/20-broken-structuredscope26.txt)):
|
|
`wrong number of type arguments; required 3`, and `FailedException` / `TimeoutException` no longer exist as nested classes
|
|
([51](output/51-api-diff-26-to-27.txt): removed `StructuredTaskScope$FailedException`, `$TimeoutException`; added `$CancelledByTimeoutException`).
|
|
|
|
Prev: [6. Lazy constants](06-lazy-constants.md) · Next: [8. PEM](08-pem-api.md)
|