Structured concurrency, a new proposal incubating in the OpenJDK community, would treat multiple tasks running in different Java threads as a single unit of work. Credit: KieferPix / Getty Images 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. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos