Java 19 Officially released on September 20, 2022, Java 19 is not a long term support release until March 2023 when it will be replaced by JDK 20, this update brings a total of 7 new features.
OpenJDK Java 19 download: https://jdk.java.net/19/
OpenJDK Java 19 documentation: https://openjdk.java.net/projects/jdk/19/
7 new features brought by Java 19.
JEP 405: Record Pattern matching (preview)
record is a new type, it is essentially a
final class, while all properties are
final modified, it will automatically compile
toString and other methods, reducing the amount of code written. record was proposed in Java 14 in Java 14, previewed in Java 15, and released in Java 16.
Example: Write a Dog record class, defining name and age properties.
The use of Record.
In Java 19, enhanced pattern matching was brought to Record to allow type conversion after using
You can even get a reference to a variable in
Record directly when using
JEP 425: Virtual Threads (preview)
A very useful new feature, introduced gradually from Java 19, virtual threads are lightweight threads that can improve system throughput while significantly reducing the amount of code written and improving maintainability.
Thread has always been a very important part of Java concurrent programming. Thread is the concurrency unit in Java, and each Thread thread provides a stack to store local variables and method calls, as well as information about the thread context.
But the problem is that threads, like processes, are an expensive resource, and the JDK implements Threads as wrappers for OS threads, which means they are expensive and limited in number. That’s why we use thread pools to manage threads and limit the number of threads. For example, the commonly used Tomcat will use a separate thread for each request and limit the number of threads processing the request to prevent it from crashing due to too many threads; this is likely to run out of threads before the CPU or network connection is exhausted, thus limiting the throughput of the web service.
You might say that you can abandon the one-to-one approach of requests and threads and use asynchronous programming to solve this problem by segmenting the request processing and combining them into sequential pipelines that are managed through a set of APIs so that a limited number of threads can be used to handle more requests than the number of threads. This is certainly possible, but the attendant problems are
- Additional learning of asynchronous programming is required.
- Increased code complexity, which amounts to abandoning the language’s basic sequential combinatorial operations.
- Stack context information becomes difficult to track.
- Debug is difficult.
- Conflicts with the programming style of the Java platform itself, where the Java concurrency unit is a Thread, and this is an asynchronous pipeline.
For all the above reasons, Java 19 introduced virtual threads, which are no different from Threads in terms of experience and are compatible with the previous API, but in comparison they take up very few resources and optimize the efficiency of hardware usage, so they are very easy to use and do not need to be pooled.
Here is an example, create 100,000 threads, then all sleep for 1 second and finally print the time consumed, if you open the traditional Thread thread way, resources are very tight; if the thread pool way, there must be some threads waiting for thread release; but using the virtual thread way, can be completed instantly.
After execution, we found that the execution finished in 1.3 seconds, which is amazingly fast.
Note: Virtual threads only increase the throughput of the program, they do not increase the processing speed of the program.
JEP 427: switch pattern matching (three previews)
Switch pattern matching was introduced in Java 17, previewed twice in Java 18, and now previewed three times in Java 19, with the same functionality as in Java 18 New Features - Switch, the improved Switch pattern matching can be more concise code and clearer logic, here are some usage examples to compare.
The following are a few examples.
And after Java 17, it can be improved by writing the following.
switch can be combined with
null to determine.
Complex expressions can be added to case when
The type determination can be done when case
Extension: JEP 406: Type Matching for Switch preview
JEP 422: Linux/RISC-V Port
RISC-V is a free and open source RISC Instruction Set Architecture (ISA), in fact RISC-V is a series of related ISAs that are now supported by Java 19.
JEP 424: External Functions & Memory API (preview)
This feature introduces an API that allows Java developers to interact with code and data outside of the JVM by calling external functions (outside of the JVM) and safely accessing external memory (not managed by the JVM), allowing Java programs to call native libraries and work with native data without as many security risks as JNI.
This is not a new feature, it has been introduced since Java 14, and this time it has been optimized for performance, versatility, security, and ease of use.
- Java 14 JEP 370 introduced the external memory access API (incubator).
- Java 15 JEP 383 introduced external memory access API (second incubator).
- Java 16 JEP 389 introduces the external linker API (incubator).
- Java 16 JEP 393 introduces the external memory access API (third incubator).
- Java 17 JEP 412 introduces the external function and memory API (incubator).
- Java 18 JEP 419 introduces external functions and memory API (secondary incubator).
JEP 426: Vector API (Quadruple Incubator)
Achieves superior performance compared to equivalent scalar computation by reliably compiling at runtime to a vector computation representation of vector instructions on supported CPU architectures. This feature has been incubated for the fourth time and has been described in previous Java 16 ~ Java 18, so it is not repeated here.
JEP 428: Structured Concurrency (Incubation)
Simplifies error handling and cancellation, improves reliability and enhances observability by simplifying multi-threaded programming and treating multiple tasks running in different threads as a single unit of work.