Java 19 is officially released, improving the difficulty of multithreaded, concurrent programming

Java 19 is officially released, improving the difficulty of multithreaded, concurrent programming

Java 19 is officially released, improving the difficulty of multithreaded, concurrent programming

Java 19 was officially released a few minutes ago, and this is a non-LTS (Long Term Support) release. The seven features of this release include a preview of structured concurrency, logging mode, external functions, and in-memory APIs, as well as support for the open source instruction set architecture (ISA).Linux/RISC-V

New characteristics

JEP 405 Record Patterns (Preview)

JEP 422 Linux/RISC-V Port

JEP 424 Foreign Function & Memory API (预览)

JEP 425 Virtual Threads (Preview)

JEP 426 Vector API (4th incubation)

JEP 427 Pattern Matching for switch (third preview)

JEP 428 Structured Concurrency (incubation)

JEP 405 Record Patterns

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

In JDK 16, we can already implement the following features: touch screen lcd

// jdk 16 以前
if (o instanceof String) {
// 及时类型匹配 依然需要转换
String s = (String)o;
}

// jdk 16 以后
if (o instanceof String s) {
// 直接使用 s
}
However, the above attribute application is not very silky in the Record Class, and it is still necessary to obtain the property value through the property method:

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

// record 结合类型匹配 依然需要通过方法获取属性
private void print(Object object) {
if (object instanceof Position position) {
int x = position.x();
int y = position.y();
}
}
In JEP 405 we can look like this:

private void print(Object object) {
if (object instanceof Position(int x, int y)) {
// 直接使用 x 和 y
}
}
Of course, this is only a small part of JEP 405, and this feature can also be applied to statements and even nested conditions.switch

JEP 422 Linux/RISC-V Port

Due to the increasing number of hardware for the RISC-V instruction set architecture, ports for that architecture 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 is now being developed in collaboration 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, the porting of the JDK will be valuable.

JEP 424 Foreign Function & Memory API

This feature enables Java programs to interoperate with code and data outside the Java runtime through APIs. By efficiently calling external functions (i.e., code outside the JVM) and securely accessing external memory (i.e., memory that is not managed by the JVM), the API enables Java programs to call native libraries and process native data, which is more secure than using JNI. This JEP is not the first preview, since JDK 14 successive related features have been incubated and previewed, this time is an improvement on the previous related preview features.

JEP 425 Virtual Threads

Virtual thread, about the virtual thread before the fat brother has an article to specifically explain the popular science, interested can be learned through this article.

Java 19 is officially released, improving the difficulty of multithreaded, concurrent programming

Traditional threads and virtual threads

 

In this Java 19, virtual threads are officially unveiled in a preview state, which simplifies the operation of multiple threads and makes the previously “expensive” threads more “cheap”.

JEP 426 Vector API

Introduce an API to express vector computations that can be reliably compiled at runtime into best-vector instructions on supported CPU architectures, resulting in better performance than equivalent scalar computations. This feature is already an old face, and it is already the fourth preview.

JEP 427 Pattern Matching for switch

switchThe matching pattern of the statement has not yet been positiveized, and this is its third preview. Fat Brother has introduced this feature many times, you can go to Fat Brother’s previous JDK articles to see.

JEP 428 Structured Concurrency

Structured concurrency, sounds like a great look. Simplify multithreaded programming by introducing APIs for structured concurrency. Structured concurrency treats multiple tasks running in different threads as a single unit of work, simplifying error handling and cancellation, improving reliability, and enhancing observability. This is an incubation API.

The principal class of the Structured Concurrency API is. This class allows developers to build tasks into a series of concurrent subtasks and coordinate them as a unit. Subtasks execute in their own threads by forking them separately and then joining them as a unit, potentially canceling them as a unit. Successful results or exceptions for child tasks are aggregated and handled by the parent task. Limit the lifecycle of a subtask or fork to an explicit lexical scope so that we can write multithreaded code in the same way as if it were single-threaded code. Here’s an example:StructuredTaskScopeStructuredTaskScope

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());
}

}

By hmimcu