Java 20 Project Loom updates set stage for Java LTS
Java 20 reincubates two Project Loom scalability features, making them prime candidates to become standard in September's Java LTS.
This week's Java 20 release revised two Project Loom features that experts expect to have far-reaching effects on the performance of Java apps, should they become standard in September's long-term support version.
The features are part of a broader set of projects undertaken within the Open Java Development Kit (OpenJDK) open source community: Project Loom, for performance and efficiency; Project Amber language improvements for developer productivity; Project Leyden, which seeks to reduce Java startup time, warmup time and footprint; Panama, for connecting with non-Java code; Valhalla, for better memory throughput and performance; and the ZGC low-latency garbage collector.
Project Loom features that reached their second preview and incubation stage, respectively, in Java 20 included virtual threads and structured concurrency. Previews are for features set to become part of the standard Java SE language, while incubation refers to separate modules such as APIs. The second of these stages is commonly the last development phase before incorporation as a standard under OpenJDK.
Virtual threads represent a lighter-weight approach to multi-threaded applications than the traditional Java model, which uses one thread of execution per application request. This was the most efficient approach when application performance was typically limited by the capacity of server CPUs, but as CPUs have become more powerful, applications are limited by I/O, according to the number of operating system threads available.
"Before Loom, we had two options, neither of which was really good," said Aurelio Garcia-Ribeyro, senior director of project management at Oracle, in a presentation at the Oracle DevLive conference this week. "We could assign one thread to each request, [which] will make the code easier to read … but you will be wasting resources. Or you can call around this one thread per request restriction and make it so that a single OS thread can handle multiple requests … for much better CPU utilization, but it comes at a terrible price -- the programming model is now very unintuitive."
Virtual threads propose a third option. The Java virtual machine (JVM) brokers the connection between virtual threads, still assigned individually to each request, and the underlying operating system threads, optimizing CPU utilization while preserving the readability and traceability of code, Garcia-Ribeyro said.
The structured concurrency API is also designed to preserve order in multi-threaded environments by treating multiple tasks running in individual threads as a single logical unit of work. Without it, multi-threaded applications are more error-prone when subtasks are shut down or canceled in the wrong order, and harder to understand, he said.
"The principle for structured concurrency is quite straightforward -- when there is sequential code that splits into concurrent flows, they must join back in the same code unit," Garcia-Ribeyro said. "If you write code in this way, then the error handling and cancellation can be streamlined and it makes it much easier to read and debug."
Project Loom could help Java keep pace with web apps
Neither of these features saw substantial changes from Java 19 in their second preview and second incubator versions under Java 20 this week, raising the likelihood they could be considered stable enough for general availability in September's Java 21 long-term support (LTS) version.
Marcus HellbergVice president of developer relations, Vaadin
If they do, they could help the mature Java language keep up with newer web applications, said Marcus Hellberg, vice president of developer relations at Vaadin, makers of a Java web development framework based in Finland.
"It would allow a web server to handle more requests at a given time while I/O bound, waiting for a database or another service," Hellberg said. "Java is used very heavily on the back end in business applications, which is where we focus on helping businesses. … If we want to maintain and help people build new stuff, it's important that the language keeps up with that."
Virtual threads under Project Loom also require minimal changes to code, which will encourage its adoption in existing Java libraries, Hellberg said.
"Apps might see a big performance boost without having to change the way their code is written," he said. "That's very appreciated by our customers who are building software for not just a year or two, but for five to 10 years -- not having to rewrite their apps all the time is important to them."
Still, while code changes to use virtual threads are minimal, Garcia-Ribeyro said, there are a few that some developers may have to make -- especially to older applications.
"There are two scenarios in which a virtual thread cannot be unmounted, because it's blocking: when it executes code inside a synchronized block or method, or when it's calling a native method or a foreign function," he said. "The Java team has spent a lot of time and effort updating the code of the JDK itself so that we think the Java libraries don't have this problem; still, some of your own code might have this issue."
Some developers at last year's JavaOne conference said they'd like to see the language evolve more toward functional, rather than object-based programming, to keep up with newer languages, but virtual threads could represent a way for the object-oriented model to keep pace, Hellberg said.
"Leveraging that model, you could build apps that, in terms of utilizing resources, are on par with an asynchronous or reactive programming model," he said. "It's interesting to see these competing models, and in general just getting improvements in the existing system."
'Conservative and innovative'
Java 20 contained five more major JDK enhancement proposal updates under Projects Loom, Amber and Panama, and a few other Project Amber features were discussed as future roadmap items for Java 21. Overall, none of Java 20's features were "earth-shattering," according to Andrew Cornwall, an analyst at Forrester Research, but all stand to play an important role in updating Java for the 21st century.
"When I looked at what one of the first slides was at the [Oracle DevLive] keynote, the thing that stuck out to me was 'conservative and innovative,'" Cornwall said. "Conservative was first -- they are not looking to upset any of the existing Java programmers with features that are going to break a lot of what they do. But they are looking to do some innovation."
That innovation has increased under the six-month release cadence introduced with Java 10 in March 2018, Hellberg said.
"Every six months, we're getting new features out, and the fact that they're able to preview them at an earlier stage [before incorporating them into the language] has helped Java start catching up to what other languages have been doing," he said.
Beth Pariseau, senior news writer at TechTarget, is an award-winning veteran of IT journalism. She can be reached at [email protected] or on Twitter @PariseauTT.