跪拜 Guibai
← All articles
Backend · Java · Java EE

JDK 27 Structured Concurrency: Custom Exceptions, Timeouts, and Early Cancellation

By 考虑考虑 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Java teams get compile-time-typed business exceptions from subtask failures and a predicate-based early-cancel, but the API remains a preview feature behind --enable-preview, so production adoption is blocked until it's finalized. The more declarative failure handling removes a whole class of catch-and-wrap boilerplate for those who do opt in.

Summary

JDK 27's structured concurrency (JEP 533, seventh preview) tightens the API around failure handling. The new allSuccessfulOrThrow and anySuccessfulOrThrow factory methods on Joiner accept a Function<Throwable, R_X> that maps a subtask failure to a business exception, so join() throws that typed exception instead of a generic ExecutionException or Throwable. The type parameter R_X makes the exception type known at compile time, eliminating the need for catch-all and rethrow.

The API also adds allUntil(Predicate), which cancels the remaining subtasks as soon as one completes and the predicate holds, while join() returns all subtask states for inspection. open() now accepts a configuration lambda for timeouts; when a timeout fires, join() throws the same R_X. StructuredTaskScope itself changed from an abstract class to an interface, and Joiner's result() and timeout() became abstract for custom joiners.

The feature remains behind --enable-preview and is not recommended for production. The demo shows JDK 27 behavior, with changes from JDK 26 verified via javap.

Takeaways
— allSuccessfulOrThrow and anySuccessfulOrThrow map subtask failures to custom business exceptions via Function<Throwable, R_X>, so join() throws the mapped type instead of ExecutionException.
— allUntil(Predicate) cancels the remaining subtasks when any completes and the predicate holds, and join() returns all Subtask states for inspection.
— StructuredTaskScope now has three type parameters <T, R, R_X extends Throwable>, and join() only throws R_X, making the exception type known at compile time.
— StructuredTaskScope changed from an abstract class to an interface, while Joiner's result() and timeout() became abstract methods, enabling custom joiners.
— open(joiner, config) supports timeout configuration via a UnaryOperator<Configuration>, and a timeout triggers joiner.timeout() which also throws R_X.
Conclusions

Putting the exception type in the scope's type parameters moves failure handling from a runtime catch to a compile-time contract, which forces callers to decide what a subtask failure means for their business flow before the code runs.

Making StructuredTaskScope an interface instead of an abstract class signals the API is being opened for custom join logic, not just the ready-made joiners.

Concepts & terms
Structured Concurrency
A concurrency model where task lifecycles are bound to a syntactic scope: all subtasks must complete or be cancelled before the scope exits. In JDK 27's preview, StructuredTaskScope provides the scoping and Joiner defines how results are aggregated.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗