Skip to main content

Java 21

核心总结

  • LTS。虚拟线程、Record Patterns、switch 模式匹配转正,Sequenced Collections 加入,Generational ZGC 可用。

升级关注

  • LTS。Virtual Threads 已转正,但 pinning、ThreadLocal、连接池容量等仍需重新评估。
  • String Templates 后续路线变化,避免过早固化。

示例

Virtual Threads 正式

try (var executor = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1000; i++) {
executor.submit(() -> blockingCall());
}
}

Sequenced Collections

java.util.SequencedCollection<String> names = new java.util.ArrayList<>();
names.addFirst("first");
names.addLast("last");
System.out.println(names.reversed());

Record Pattern + switch Pattern

record Point(int x, int y) {}

static String render(Object value) {
return switch (value) {
case Point(int x, int y) -> x + "," + y;
case null -> "null";
default -> "other";
};
}

JEPs

JEP状态分类标题中文描述
JEP 430预览语言String Templates (Preview)字符串模板预览,后续路线发生变化,使用前需关注状态。
JEP 431正式库/APISequenced Collections统一有序集合接口。
JEP 439正式GC/RuntimeGenerational ZGC分代 ZGC。
JEP 440正式语言Record PatternsRecord 模式转正。
JEP 441正式语言Pattern Matching for switchswitch 模式匹配转正。
JEP 442第三次预览Panama/向量/原生Foreign Function & Memory API (Third Preview)FFM API 第三次预览。
JEP 443预览语言Unnamed Patterns and Variables (Preview)未命名模式和变量预览,用 _ 表示不用的值。
JEP 444正式并发/LoomVirtual Threads虚拟线程转正。
JEP 445预览语言Unnamed Classes and Instance Main Methods (Preview)未命名类和实例 main 方法预览,降低入门样板。
JEP 446预览并发/LoomScoped Values (Preview)Scoped Values 进入预览。
JEP 448第六次孵化Panama/向量/原生Vector API (Sixth Incubator)Vector API 第六次孵化。
JEP 449废弃工具/平台Deprecate the Windows 32-bit x86 Port for Removal废弃 Windows 32-bit x86 port。
JEP 451迁移预告库/APIPrepare to Disallow the Dynamic Loading of Agents准备限制运行时动态加载 Agent。
JEP 452正式库/APIKey Encapsulation Mechanism APIKEM 密钥封装机制 API。
JEP 453预览并发/LoomStructured Concurrency (Preview)结构化并发进入预览。

References