Multi-threaded programming can be made easier for Java developers with a plan that is currently being incubated in the OpenJDK community.
The proposal for structured parallelism will introduce a library that treats multiple tasks running in different threads as a single unit of work. The new library will streamline error handling and reversal, improving reliability and improving traceability, as proposed. The objectives of the plan include improving the reliability and traceability of multi-threaded code and promoting a simultaneous programming style that can eliminate common risks arising from cancellation and shutdown, such as thread leakage and delay cancellation. At this time, the structured parallelism proposal does not target a specific version of Java.
Structured parallelism is an approach to multi-threaded programming that preserves developers’ experience of readability and support with single-threaded code, the proposal said. It is based on the principle that if a task is divided into simultaneous subtasks, they all return to the same place: the code block of the task. By returning to the same code block, the life of a simultaneous subtask is limited to a syntactic block. Because sibling subtasks are limited to the same block, they can be justified and managed as a unit. Subtasks work on behalf of a task– code in the enclosure – which waits for results and monitors them for failures. As with structured single-threaded code programming techniques, the power of structured multi-threaded parallelism comes from two ideas: well-defined input and output points for execution flow through a block of code, and strictly embedding the life of operations in a way that reflects insertion into code.
During execution, structured parallelism builds a tree-like hierarchy of tasks, with brother and sister subtasks being the property of the same parent task. The tree is the parallel double of the call stack on a single thread.
Structured parallel is virtual thread matching, which is a lightweight implementation of threads provided by JDK. A preview of the virtual threads is scheduled for Java 19 this September. Many virtual threads share the same OS thread, allowing a large number of virtual threads. They can be a simultaneous unit of behavior, even I / O behavior. In this way, the server application can use structured concurrency to process thousands or millions of incoming requests at once.
In essence, virtual threads deliver an abundance of threads, and structured parallelism ensures that they are properly coordinated. The presence of a structured parallelism library in the JDK offers server-side developers support and reliability.
The proposal does not include the replacement of structures for parallelism in
java.util.concurrent or provide a final structured parallel API for Java. The proposal also does not plan to add a mechanism for sharing data flows between threads, although this may be considered in the future. The existing thread termination mechanism will not be replaced by a new thread cancellation mechanism under this proposal, but this may also happen in the future.
Copyright © 2022 IDG Communications, Inc.