JDK19 features


Insert image description here

JAVA19 overview

JDK 19 was officially released for production use on September 20, 2022, and is not a long-term support version. However, there are some important new features in JDK 19 that are worthy of attention.

image.png

JDK 19 has only 7 new features:

  • JEP 405: Record Patterns[1] (Preview)
  • JEP 422: Linux/RISC-V Port[2]
  • JEP 424: Foreign Function & Memory API [3] (Preview)
  • JEP 425: Virtual Threads [4] (preview)
  • JEP 426: Vector API[5] (Fourth incubation)
  • JEP 427: Pattern Matching for switch (switch pattern matching)[6]
  • JEP 428: Structured Concurrency [7] (Incubation)

1. Recording mode (preview version)

Enhance the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable powerful, declarative and composable forms of data navigation and processing.

2.Linux/RISC-V porting

Porting the JDK to Linux/RISC-V, currently only supports the RV64GV configuration of RISC-V (generic 64-bit ISA containing vector instructions). Support for other RISC-V configurations, such as the generic 32-bit configuration (RV32G), may be considered in the future.

3. External functions and memory API (preview version)

Introduces an API through which Java programs can interoperate with code and data outside the Java runtime. This API
can effectively call external functions (code outside the JVM) and safely access external memory (memory not managed by the JVM), allowing Java
programs to call native libraries and process native data without JNI. vulnerabilities and dangers.

4. Virtual thread (preview version)

image.png

There is a one-to-one correspondence between JVM threads and operating system threads. The actual execution of the thread needs to wait until the OS allocates resources for scheduling. Therefore, the operating system's limit on the number of threads also affects the number of JVM threads. How to modify the number of operating system threads in Centos7.

# vim /etc/security/limits.d/20-nproc.conf
* soft nproc 65535
* hard nproc 65535

Virtual thread:

image.png

In response to the above-mentioned JVM thread limitation problem, JDK19 provides a virtual thread method, which can break through the limit of the number of threads to a certain extent (drawing on the design ideas of golang goroutine). In JDK19, threads are divided into platform threads (Platform Thread) and virtual threads (Virtual Thread).

As shown in the figure above, virtual threads are completely managed by the JVM and are not restricted by the operating system. Therefore, the JVM can generate a large number of virtual threads to execute business logic, thus improving the throughput of the system. But, but, but (say important things three times), this feature is not mature yet and is not recommended for use in production environments.

Case code:

    public static void main(String[] args) throws Exception {
    
    
        for (int i = 0; i < 100; i++) {
    
    
            // 启动虚拟线程
            Thread.startVirtualThread(()->{
    
    
                System.out.println(Thread.currentThread() );
            });
        }
        Thread.sleep(10000);
        System.out.println("Hello ...");
    }

Related API description:

  • Thread.ofVirtual() creates a virtual thread
  • Thread.ofPlatform() creates a platform thread
  • Thread.startVirtualThread(Runnable) creates and runs a virtual thread
  • Executors.newVirtualThreadPerTaskExecutor() thread pool

test code

public class Test03 {
    
    

    public static void main(String[] args) throws Exception {
    
    
        //ExecutorService executorService = Executors.newFixedThreadPool(1);
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        executorService.execute(new MyTask());
        executorService.execute(new MyTask());
        executorService.execute(new MyTask());
        executorService.execute(new MyTask());
        executorService.execute(new MyTask());

        Thread.sleep(2000);
    }

    static class MyTask implements Runnable{
    
    
        @Override
        public void run() {
    
    
            System.out.println(Thread.currentThread());
            try {
    
    
                Thread.sleep(1000);
            }catch (Exception e){
    
    
                e.printStackTrace();
            }
            System.out.println(LocalDateTime.now());
        }
    }

}

Modify JVM parameters

-Djdk.virtualThreadScheduler.parallelism=1 -Djdk.virtualThreadScheduler.maxPoolSize=1

Set the number of core threads and the maximum number of threads of ForkJoinPool

  • -Djdk.virtualThreadScheduler.parallelism=1
  • -Djdk.virtualThreadScheduler.maxPoolSize=1

5.Vector API (fourth incubation)

Introduces an API to express vector computations that compile reliably at runtime, optimizing vector instructions on supported CPU architectures to achieve performance superior to scalar computations.

6.Switch pattern matching (third preview version)

Enhance the Java programming language with switchpattern matching of expressions and statements, and extensions to the pattern language. Pattern matching is extended switchto allow expressions to be tested against a number of patterns, allowing complex data-oriented queries to be expressed concisely and safely.

This feature first appeared in Java 17 as a preview, and Java 19 is the third preview.

7. Structured concurrency (incubation phase)

Simplify multithreaded programming by introducing an API for structured concurrency, which treats multiple tasks running in different threads as a single unit of work, simplifying error handling, improving reliability, and enhancing observability.

Guess you like

Origin blog.csdn.net/qq_28314431/article/details/132950854