Java 20: a faster future is Looming

Java 20: a faster future is Looming

The new six-month release cadence that Oracle announced for Java in 2018 marked the beginning of a new era for the language. Instead of getting big, breaking releases every few years, we now receive a steady stream of new features and improvements.

This week, Oracle released Java 20 with some exciting preview features building up to a significant performance boost in the next LTS release, Java 21.

This week, I had the opportunity to attend the Java 20 launch event at Oracle HQ in Redwood City and hear from the team working on the new features.

Georges Saab presenting on a stage.

Java: A Thoughtful Evolution

Georges Saab, Senior VP of Development for the Java Platform, explained that Java's development is a balancing act between conservatism and innovation.

On one hand, they want to maintain compatibility and not alienate people. On the other hand, they want to adapt to change and fix mistakes.

The new release model allows Java to ship new features in smaller increments and make new APIs available to developers earlier. This way, they can receive feedback from the community and make changes before finalizing them.

A graph showing the number of features shipped per release. Releases prior to the new 6-month cycle had tons more changes.

Improved Developer Productivity

Project Amber is a collection of features aimed at improving developer productivity. The project has already delivered several improvements to the language, such as local variable type inference (var), records, and text blocks.

Java 20 includes previews of two pattern-matching features: record patterns and pattern matching for switch.

Record patterns allow you to match the fields of a record, including nested records:

if (p instanceof Person(String name, Address(String street, String city, String country))) {
    // do something with name, street, city, country
}

for (Point(var x, var y) : points) {
    // do something with x and y
}

Pattern matching for switch allows you to match on a pattern and cast the value to a specific type, optionally with a guard expression:

switch (o) {
  case String s when s.length() == 1 -> ...
  case "hello" -> ...
  case String s -> ...
  default -> ...
}

Massive Scaling with Virtual Threads

Project Loom is a major initiative to improve the scalability of Java applications. It introduces a new type of thread called a virtual thread, enabling high-throughput, low-latency, and low-overhead concurrency.

Virtual threads are lightweight and don't require a native thread to be created. This means that you can create millions of virtual threads without running out of native threads. This is especially useful for applications that need to handle a lot of concurrent requests, such as web servers.

The aim of Project Loom is to match the performance of reactive frameworks with a simpler programming model.

Jetty performance using native threads vs virtual threads.

Impressively, existing code should just work with virtual threads.

Sergey Kuksenko shared five tips for adopting virtual threads:

  1. Use simple, blocking I/O APIs

  2. Start a new thread per task instead of using shared thread pools

  3. Never pool virtual threads; use semaphores to limit concurrency

  4. Don't cache objects in ThreadLocals

  5. Avoid pinning

Connecting the JVM with Native Code

[Project Panama] is a collection of features that allow you to connect the JVM with native code. This is useful for performance-critical applications that need to call native libraries.

Java 20 includes two preview features: Foreign Function and Memory API and Vector API.

While both are APIs that will be used directly by a small number of developers, they can have a significant impact in some areas as they get adopted in libraries and frameworks.

Java 21: The Next LTS Release

The major announcement at the event was that Java 21 will be the next LTS release. Although it wasn't confirmed, it seemed clear that Oracle hopes it will be the first release to include Project Loom.

A Thriving Community

Java has a strong community behind it, with 10 million Java developers, 60 billion active JVMs (38 billion cloud-based JVMs).

Independent contributors make up the second-largest group of contributors.

OpenJDK contributors. Independent developers are the second-largest group.

It was great to see that Oracle is continuing to invest in growing the community, with initiatives such as the Java Champions program. They have also recently hired Heather Stephens to head up their Java in education strategy, ensuring that Java remains a relevant language for the next generation of developers.

How Will the New Features Affect Vaadin Users?

Project Loom has the potential to improve the performance of Vaadin applications by allowing application servers to handle more concurrent requests. Vaadin Flow apps are rarely I/O bound, so the benefits of virtual threads are likely to be limited. Hilla applications, on the other hand, are more likely to benefit from performance improvements.

The new language features will benefit all Java developers, including Vaadin developers.