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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos