JDK 27 Structured Concurrency: Custom Exceptions, Timeouts, and Early Cancellation
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.
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.
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.