Java 19 officially released, improve multi-threaded, concurrent programming difficulties

java twitter

Java 19 was officially released a few minutes ago, a non-LTS (long term support) release with seven features including structured concurrency, record mode, a preview of external functions and memory APIs, and support for the open source Linux/RISC-V instruction set architecture (ISA).

New Features

  • JEP 405 Record Patterns (preview)

  • JEP 422 Linux/RISC-V Port

  • JEP 424 Foreign Function & Memory API (preview)

  • JEP 425 Virtual Threads (Preview)

  • JEP 426 Vector API (4th incubation)

  • JEP 427 Pattern Matching for switch (3rd preview)

  • JEP 428 Structured Concurrency (incubation)

JEP 405 Record Patterns

This is an enhancement to the Record Class that went into effect in Java 17. JEP 405 allows record patterns and type patterns to be nested for powerful, declarable, composable forms of data processing.

In JDK 16, we could already implement the following features.

// jdk 16 ago
if (o instanceof String) {
    // Just-in-time type matching still requires conversion
    String s = (String)o;     

// jdk 16 onwards
if (o instanceof String s) {
    // Use s directly 

But the above feature is not very silky when applied to Record Class, it still requires the property method to get the property value.

public record Position(int x, int y) {}

// Combined with type matching, you still need to get the property by method
private void print(Object object) {
  if (object instanceof Position position) {
    int x =  position.x();
    int y =  position.y();

In JEP 405 we can have this.

private void print(Object object) {
  if (object instanceof Position(int x, int y)) {
      // Use x and y directly

Of course this is only a small part of JEP 405, the feature can also be applied to switch statements, or even nested conditions.

JEP 422 Linux/RISC-V Port

Due to the increasing availability of hardware for RISC-V instruction set architectures, ports for the corresponding architectures are available starting with Java 19.

RISC-V is a free and open source RISC instruction set architecture (ISA) originally designed by the University of California, Berkeley, and now developed collaboratively under the auspices of RISC-V International. It is already supported by a wide range of language toolchains. With the increasing popularity of RISC-V hardware, a port of JDK would be valuable.

JEP 424 Foreign Function & Memory API

This feature allows Java programs to interoperate with code and data outside of the Java runtime through the API. By effectively calling external functions (i.e., code outside the JVM) and safely accessing external memory (i.e., memory not managed by the JVM), the API enables Java programs to call native libraries and manipulate native data more safely than using JNI. This JEP is not the first preview, relevant features have been incubated and previewed one after another since JDK 14, this is an improvement to the previous relevant preview features.

JEP 425 Virtual Threads

Traditional threads and virtual threads

In Java 19 virtual threads are officially presented as previews, which simplify the operation of multiple threads and make the previously “expensive” threads more “affordable”.

JEP 426 Vector API

Introduces an API to express vector computations that can be reliably compiled at runtime to the best vector instructions on the supported CPU architecture, achieving better performance than equivalent scalar computations. This feature is an old face, and is in its fourth preview.

JEP 427 Pattern Matching for switch

Pattern matching for switch statements, which has not yet been converted, is in its third preview. Fat has introduced the feature many times, you can go to Fat’s previous JDK release to see the article.

JEP 428 Structured Concurrency

Structured Concurrency, sounds awesome. Simplify multi-threaded programming by introducing an API for structured concurrency. Structured concurrency treats multiple tasks running in different threads as a single unit of work, thereby simplifying error handling and cancellation, improving reliability, and enhancing observability. This is an incubation API.

The main class of the Structured Concurrency API is StructuredTaskScope. This class allows developers to construct tasks as a series of concurrent subtasks and coordinate them as a unit. Subtasks are executed in their own thread by forking them individually (fork), then joining them as a unit, and possibly canceling them as a unit (cancel ). Successful results or exceptions of subtasks are aggregated and handled by the parent task. The StructuredTaskScope restricts the life cycle of a subtask or fork to explicit lexical scope, so that we can write multi-threaded code as if it were single-threaded code. An official example is given here.

Response handle() throws ExecutionException, InterruptedException {
    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
        Future<String>  user  = scope.fork(() -> findUser());
        Future<Integer> order = scope.fork(() -> fetchOrder());

        scope.join();           // Join both forks
        scope.throwIfFailed();  // ... and propagate errors

        // Here, both forks have succeeded, so compose their results
        return new Response(user.resultNow(), order.resultNow());

Java’s multi-threaded programming is a nightmare for developers, and this feature promises to improve the situation.