Multithreaded programming could be about to get easier for Java developers under a plan currently incubating in the OpenJDK community.
The structured concurrency proposal would introduce a library that treats multiple tasks running in different threads as a single unit of work. The new library would streamline error handling and cancellation, improving reliability and enhancing observability, according to the proposal. Goals of the plan include improving the reliability and observability of multithreaded code and promoting a concurrent programming style that can eliminate common risks arising from cancellation and shutdown, such as thread leaks and cancellation delays. At this point, the structured concurrency proposal is not targeted for a specific version of Java.
Structured concurrency is an approach to multithreaded programming that preserves the readability and maintainability developers experience with single-threaded code, the proposal states. It carries the principle that if a task splits into concurrent subtasks, they all return to the same place: the task’s code block. By returning to the same code block, the lifetime of a concurrent subtask is confined to a syntactic block. Because sibling subtasks are confined to the same block, they can be reasoned about and managed as a unit. Subtasks work on behalf of a task–code in the enclosing block– that awaits results and monitors them for failures. As with structured programming techniques for single-threaded code, the power of structured concurrency for multiple threads comes from two ideas: well-defined entry and exit points for the flow of execution through a block of code, and strictly nesting the operations’ lifetime in a way that mirrors nesting in the code.
At runtime, structured concurrency builds a tree-shaped hierarchy of tasks, with sibling subtasks owned by the same parent task. The tree is the concurrent counterpart to the call stack of a single thread.
Structured concurrency is a match for virtual threads, which is a lightweight implementation of threads provided by the JDK. A preview of virtual threads is planned for Java 19 this September. Many virtual threads share the same OS thread, allowing for a large number of virtual threads. These can represent a concurrent unit of behavior, even I/O behavior. Thus, a server application could use structured concurrency to process thousands or millions of incoming requests at once.
In essence, virtual threads deliver an abundance of threads and structured concurrency ensures they are correctly coordinated. Having a library for structured concurrency in the JDK offers server-side developers maintainability and reliability.
The proposal does not involve replacing concurrency constructs in
java.util.concurrent or providing a definitive structured concurrency API for Java. The proposal is also not planning to add a mechanism for sharing streams of data among threads, though this might be addressed in the future. The existing thread interruption mechanism would not be replaced with a new thread cancellation mechanism under the current proposal, but that, too, might happen in the future.