Java 20, a walk through JEP lane

Samuel Owino
6 min readMar 24, 2023
Photo by Mohammad Rahmani on Unsplash

Java enthusiasts, get ready to jump for joy because Java 20 is here! This latest release brings a whole bunch of exciting new features that will make your coding experience more fun and productive than ever before. With scoped values, sharing immutable data between threads is now easier than ever, while the structured concurrency API simplifies multithreaded programming by treating multiple tasks as a single unit of work. And if that’s not enough to get your heart racing, the Foreign Function & Memory API lets you play nice with code and data outside of the Java runtime, providing a safe and efficient alternative to JNI. So buckle up and get ready for a wild ride with Java 20!

JEP 429: Scoped Values: Introduce scoped values

In Java, thread-local variables are used to maintain a state that is specific to a single thread. However, the use of thread-local variables can result in a large amount of memory overhead when using a large number of virtual threads. To address this issue, Java 20 introduced JEP 429: Scoped Values, which introduces a new way to share immutable data within and across threads.

Scoped Values

Scoped Values are a new type of variable in Java that allow for the sharing of immutable data between threads. Unlike thread-local variables, which can only be accessed within the thread they are created in, Scoped Values can be accessed by any thread that is within the same scope.

Scoped Values are created using the new ScopedValues API, which provides methods for creating and accessing Scoped Values. The ScopedValues API also provides methods for creating nested scopes, which allow for the creation of Scoped Values with different values within the same thread.

Scoped Values vs. Thread-Local Variables

Scoped Values provide a number of advantages over thread-local variables. First, Scoped Values are immutable, which means that they can be safely shared between threads without the need for synchronization. Second, Scoped Values have a smaller memory footprint than thread-local variables, which can be important when using a large number of virtual threads.

Scoped Values also provide better performance than thread-local variables when used with virtual threads. This is because Scoped Values are designed to work with virtual threads, whereas thread-local variables were not specifically designed for virtual threads.

Java 20’s JEP 429: Scoped Values introduces a new way to share immutable data within and across threads. Scoped Values provide a number of advantages over thread-local variables, including better performance and a smaller memory footprint. The new ScopedValues API provides methods for creating and accessing Scoped Values, as well as creating nested scopes. While the Scoped Values API is still incubating, it is expected to become a standard part of Java in future releases.

JEP 437: Structured Concurrency

Photo by Luis Gonzalez on Unsplash

Java 20’s JEP 437: Structured Concurrency introduces a new API for simplifying multithreaded programming by treating multiple tasks running in different threads as a single unit of work. This approach, known as structured concurrency, provides a number of benefits, including improved reliability, better error handling, and easier cancellation of tasks. JEP 437 builds upon the work done in JEP 428, which introduced the Structured Concurrency API as an incubating feature in JDK 19. This JEP re-incubates the API in JDK 20, with the addition of support for inheritance of scoped values from JEP 429.

Structured Concurrency API

The Structured Concurrency API provides a set of classes and interfaces for managing tasks and their execution. The core of the API is the CompletableTask class, which represents a single task that can be executed in a separate thread. CompletableTask objects can be composed together using the and and then methods to create complex workflows that execute in a structured and coordinated manner.

The CompletableTask class also provides methods for error handling and cancellation. When a task encounters an error, it can propagate the error to its parent task, which can then handle the error appropriately. Similarly, when a task is cancelled, it can propagate the cancellation to its child tasks, ensuring that all tasks are cancelled in a structured manner.

Scoped Values and Structured Concurrency

The re-incubated Structured Concurrency API in JDK 20 adds support for the inheritance of scoped values from JEP 429. This allows threads created in a task scope to inherit the scoped values from the parent scope, streamlining the sharing of immutable data across threads. This feature enhances the observability of tasks, allowing developers to monitor the flow of data through the system and identify potential bottlenecks or issues.

Java 20’s JEP 437: Structured Concurrency introduces a new API for simplifying multithreaded programming through the use of structured concurrency. The API provides a number of benefits, including improved reliability, better error handling, and easier cancellation of tasks. The re-incubated API in JDK 20 adds support for the inheritance of scoped values from JEP 429, streamlining the sharing of immutable data across threads and enhancing observability. While the Structured Concurrency API is still incubating, it has the potential to become a powerful tool for building complex, reliable, and scalable multithreaded applications in Java.

JEP 434: Foreign Function & Memory API

Photo by Carlos Irineu da Costa on Unsplash

Java 20’s JEP 434: Foreign Function & Memory API introduces a new API that allows Java programs to interoperate with code and data outside of the Java runtime. The API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI. JEP 434 combines two earlier incubating APIs, the Foreign-Memory Access API and the Foreign Linker API, both of which were incubated in previous versions of Java. This JEP proposes to refine the API based on feedback and re-preview it in JDK 20.

Unified MemorySegment and MemoryAddress Abstractions

One of the key changes introduced in JEP 434 is the unification of the MemorySegment and MemoryAddress abstractions. In previous versions of the API, MemoryAddress was used to represent a pointer to memory outside the JVM, while MemorySegment was used to represent a contiguous block of memory within the JVM. In the new API, MemoryAddress is now modeled as a zero-length MemorySegment, allowing for a more consistent and simplified programming model.

Enhancements to Sealed MemoryLayout Hierarchy

Another improvement in JEP 434 is the enhancement of the sealed MemoryLayout hierarchy. This enhancement facilitates usage with pattern matching in switch expressions and statements, which was introduced in JEP 433. This change improves the readability and maintainability of the code, making it easier to write and debug complex programs.

Splitting MemorySession into Arena and SegmentScope

Finally, JEP 434 splits the MemorySession class into Arena and SegmentScope to facilitate sharing segments across maintenance boundaries. This allows developers to share segments between different maintenance operations, improving the performance and scalability of the application.

Java 20’s JEP 434: Foreign Function & Memory API introduces a new API that allows Java programs to interoperate with code and data outside of the Java runtime. This API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI. JEP 434 builds on two earlier incubating APIs and includes several refinements based on feedback. The unification of the MemorySegment and MemoryAddress abstractions, enhancements to the sealed MemoryLayout hierarchy, and splitting of the MemorySession class into Arena and SegmentScope are some of the key changes introduced in this JEP. With these improvements, the FFM API has the potential to become a powerful tool for building complex and efficient applications that can interact with native code and data.

Conclusion

In conclusion, Java 20 is a major step forward for the Java programming language, offering a host of exciting new features and improvements that make it easier and more enjoyable to build high-performance and scalable applications. From simplified multithreaded programming to safer interoperation with external code and data, this release has something for everyone

--

--