Skip to content

openjdk 21 新特性验证

1 string Template(预览)test ok

测试代码:

package module1;

public class Main{
    public static void main(String[] args) throws InterruptedException{
        String message="hhh";
        System.out.println(STR."Hello, World! from \{message}");
    }
}

运行测试:

[root@localhost my-pro]# java --enable-preview --source 21 src/module1/Main.java
Note: src/module1/Main.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
Hello, World! from hhh
正常运行。

2 序列化集合 test ok

提供了新的api:addFirst,addLast,getFirst,getLast,reversed

测试代码:

import java.util.ArrayList;
import java.util.List;


public class Main {
    public static void main(String[] args) throws InterruptedException {
        ArrayList<Integer> arrayList = new ArrayList<>();

        arrayList.add(1);   // List contains: [1]

        arrayList.addFirst(0);  // List contains: [0, 1]
        arrayList.addLast(2);   // List contains: [0, 1, 2]

        Integer firstElement = arrayList.getFirst();  // 0
        Integer lastElement = arrayList.getLast();  // 2

        List<Integer> reversed = arrayList.reversed();
        System.out.println(reversed); // Prints [2, 1, 0]
    }
}

测试结果:

[root@localhost my-pro]# java src/SequentialCollect.java
[2, 1, 0]

3 分代ZGC test ok

扩展了ZGC的功能,增加了分代 GC 功能。不过,默认是关闭的,需要通过配置打开

[root@localhost my-pro]# java -XX:+UseZGC -XX:+ZGenerational src/SequentialCollect.java
[2, 1, 0]

4 switch模式匹配 test ok

测试代码,按类型进行匹配:

public class Main {
    static String formatterPatternSwitch(Object obj) {
        return switch (obj) {
            case Integer i -> String.format("int %d", i);
            case Long l    -> String.format("long %d", l);
            case Double d  -> String.format("double %f", d);
            case String s  -> String.format("String %s", s);
            default        -> obj.toString();
        };
    }
    public static void main(String[] args) throws InterruptedException {
        String s = "hello world!";
        System.out.println(formatterPatternSwitch(s));
    }
}

测试结果:

[root@localhost my-pro]# java src/SwitchReg.java
String hello world!

5 未命名变量(预览)test ok

测试代码:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        int [] test = new int[3];
        for(int _ : test){
            System.out.println("hello world");
        }
    }
}

测试结果:

[root@localhost my-pro]# java --enable-preview --source 21 src/UnamedVal.java
Note: src/UnamedVal.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
hello world
hello world
hello world

6 未命名main方法(预览) test ok

测试代码:

[root@localhost my-pro]# cat src/UnamedMain.java
void main() {
   System.out.println("Hello, World!");
}

测试结果:

[root@localhost my-pro]# java --enable-preview --source 21 src/UnamedMain.java
Note: src/UnamedMain.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
Hello, World!

7 虚拟线程(重要新特性)test ok

JVM中实现的平台线程通常是跟内核线程1:1的关系,而一个平台线程可以对应多个虚拟线程,虚拟线程可以减少线程上下文切换的开销,并充分利用CPU和内存。但虚拟线程不适合CPU密集型任务,因为最终还是CPU线程在工作,但很适合IO密集型任务。

测试代码:

public class VirtualThreadTest {
    public static void main(String[] args) {
        CustomThread customThread = new CustomThread();
        Thread virtualThread = Thread.ofVirtual().start(customThread);

        // 确保主线程等待虚拟线程完成
        try {
            virtualThread.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 恢复中断状态
            System.err.println("Main thread interrupted");
        }
    }
}

class CustomThread implements Runnable {
    @Override
    public void run() {
        System.out.println("Hello world from a virtual thread");
    }
}

测试结果:

[root@localhost my-pro]# java src/VirThread.java
Hello world from a virtual thread