Java17的新特性

3,114 阅读5分钟

Java语言特性系列

本文主要讲述一下Java17的新特性

版本号

java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment (build 17+35-2724)
OpenJDK 64-Bit Server VM (build 17+35-2724, mixed mode, sharing)

从version信息可以看出是build 17+35

特性列表

JEP 306: Restore Always-Strict Floating-Point Semantics

恢复始终执行严格模式的浮点定义,修复25年前英特尔的浮点指令存在的一些问题

JEP 356: Enhanced Pseudo-Random Number Generators

引入RandomGenerator及RandomGeneratorFactory提供更好的随机数生成

RandomGenerator generator = RandomGeneratorFactory.all()
    .filter(RandomGeneratorFactory::isJumpable)
    .filter(factory -> factory.stateBits() > 128)
    .findAny()
    .map(RandomGeneratorFactory::create)
//  if you need a `JumpableGenerator`:
//  .map(JumpableGenerator.class::cast)
    .orElseThrow();

JEP 382: New macOS Rendering Pipeline

使用Apple Metal API为macOS新增了Java 2D internal rendering pipeline

JEP 391: macOS/AArch64 Port

迁移JDK到macOS/AArch64

JEP 398: Deprecate the Applet API for Removal

标记Applet API为废弃方便后续移除,具体如下

java.applet.Applet
java.applet.AppletStub
java.applet.AppletContext
java.applet.AudioClip
javax.swing.JApplet
java.beans.AppletInitializer

JEP 403: Strongly Encapsulate JDK Internals

对JDK内部的api进行更强的封装,是JEP 396: Strongly Encapsulate JDK Internals by Default的后续

JEP 406: Pattern Matching for switch (Preview)

引入switch模式匹配的preview版本,instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16转正

static String formatterPatternSwitch(Object o) {
    return switch (o) {
        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        -> o.toString();
    };
}

JEP 407: Remove RMI Activation

移除Remote Method Invocation (RMI),它在JDK15的JEP 385被废弃

JEP 409: Sealed Classes

Sealed Classes在JDK15作为preview引入,在JDK16作为第二轮preview,在JDK17转正

package com.example.geometry;

public abstract sealed class Shape
    permits Circle, Rectangle, Square, WeirdShape { ... }

public final class Circle extends Shape { ... }

public sealed class Rectangle extends Shape 
    permits TransparentRectangle, FilledRectangle { ... }
public final class TransparentRectangle extends Rectangle { ... }
public final class FilledRectangle extends Rectangle { ... }

public final class Square extends Shape { ... }

public non-sealed class WeirdShape extends Shape { ... }

JEP 410: Remove the Experimental AOT and JIT Compiler

移除实验性的java版本的AOT及JIT Compiler,具体移除

jdk.aot — the jaotc tool
jdk.internal.vm.compiler — the Graal compiler
jdk.internal.vm.compiler.management — Graal's MBean

后续要使用可以使用GraalVM

JEP 411: Deprecate the Security Manager for Removal

废弃java1.0引入的Security Manager方便后续移除

JEP 412: Foreign Function & Memory API (Incubator)

JDK14的JEP 370: Foreign-Memory Access API (Incubator)引入了Foreign-Memory Access API作为incubator,JDK15的JEP 383: Foreign-Memory Access API (Second Incubator)Foreign-Memory Access API作为第二轮incubator,JDK16的JEP 393: Foreign-Memory Access API (Third Incubator)作为第三轮,它引入了Foreign Linker API,JDK17引入Foreign Function & Memory API

JEP 414: Vector API (Second Incubator)

JDK16引入了JEP 338: Vector API (Incubator)提供了jdk.incubator.vector来用于矢量计算,JDK17进行改进并作为第二轮的incubator

JEP 415: Context-Specific Deserialization Filters

允许应用去配置指定上下文及动态选择的deserialization filters,示例

public class FilterInThread implements BinaryOperator<ObjectInputFilter> {

    // ThreadLocal to hold the serial filter to be applied
    private final ThreadLocal<ObjectInputFilter> filterThreadLocal = new ThreadLocal<>();

    // Construct a FilterInThread deserialization filter factory.
    public FilterInThread() {}

    /**
     * The filter factory, which is invoked every time a new ObjectInputStream
     * is created.  If a per-stream filter is already set then it returns a
     * filter that combines the results of invoking each filter.
     *
     * @param curr the current filter on the stream
     * @param next a per stream filter
     * @return the selected filter
     */
    public ObjectInputFilter apply(ObjectInputFilter curr, ObjectInputFilter next) {
        if (curr == null) {
            // Called from the OIS constructor or perhaps OIS.setObjectInputFilter with no current filter
            var filter = filterThreadLocal.get();
            if (filter != null) {
                // Prepend a filter to assert that all classes have been Allowed or Rejected
                filter = ObjectInputFilter.Config.rejectUndecidedClass(filter);
            }
            if (next != null) {
                // Prepend the next filter to the thread filter, if any
                // Initially this is the static JVM-wide filter passed from the OIS constructor
                // Append the filter to reject all UNDECIDED results
                filter = ObjectInputFilter.Config.merge(next, filter);
                filter = ObjectInputFilter.Config.rejectUndecidedClass(filter);
            }
            return filter;
        } else {
            // Called from OIS.setObjectInputFilter with a current filter and a stream-specific filter.
            // The curr filter already incorporates the thread filter and static JVM-wide filter
            // and rejection of undecided classes
            // If there is a stream-specific filter prepend it and a filter to recheck for undecided
            if (next != null) {
                next = ObjectInputFilter.Config.merge(next, curr);
                next = ObjectInputFilter.Config.rejectUndecidedClass(next);
                return next;
            }
            return curr;
        }
    }

    /**
     * Apply the filter and invoke the runnable.
     *
     * @param filter the serial filter to apply to every deserialization in the thread
     * @param runnable a Runnable to invoke
     */
    public void doWithSerialFilter(ObjectInputFilter filter, Runnable runnable) {
        var prevFilter = filterThreadLocal.get();
        try {
            filterThreadLocal.set(filter);
            runnable.run();
        } finally {
            filterThreadLocal.set(prevFilter);
        }
    }
}

// Create a FilterInThread filter factory and set
    var filterInThread = new FilterInThread();
    ObjectInputFilter.Config.setSerialFilterFactory(filterInThread);

    // Create a filter to allow example.* classes and reject all others
    var filter = ObjectInputFilter.Config.createFilter("example.*;java.base/*;!*");
    filterInThread.doWithSerialFilter(filter, () -> {
          byte[] bytes = ...;
          var o = deserializeObject(bytes);
    });

细项解读

上面列出的是大方面的特性,除此之外还有一些api的更新及废弃,主要见JDK 17 Release Notes,这里举几个例子。

添加项

  • DatagramSocket Can Be Used Directly to Join Multicast Groups (JDK-8237352)

更新了java.net.DatagramSocket用于支持joining multicast groups

java.io.Console新增了方法用于返回console的charset

  • JDK Flight Recorder Event for Deserialization (JDK-8261160)

JDK Flight Recorder新增了jfr.Deserialization实现

  • Unified Logging Supports Asynchronous Log Flushing (JDK-8229517)

引入了-Xlog:async参数用于异步日志,以及-XX:AsyncLogBufferSize=<bytes>用于控制buffer的大小

移除项

  • Removal of sun.misc.Unsafe::defineAnonymousClass (JDK-8243287)

移除sun.misc.Unsafe::defineAnonymousClass方法

废弃项

废弃了Kerberos的des3-hmac-sha1及rc4-hmac这两个encryption types

  • Deprecate the Socket Implementation Factory Mechanism (JDK-8235139)

废弃了如下几个工厂

static void ServerSocket.setSocketFactory​(SocketImplFactory fac)
static void Socket.setSocketImplFactory​(SocketImplFactory fac)
static void DatagramSocket.setDatagramSocketImplFactory​(DatagramSocketImplFactory fac)

已知问题

  • TreeMap.computeIfAbsent Mishandles Existing Entries Whose Values Are null (JDK-8259622)

TreeMap.computeIfAbsent方法针对null的处理与规范有偏差

  • Segmentation Fault Error on 9th and 10th Generation Intel® Core™ Processors (JDK-8263710)

当运行在9th and 10th Generation Intel® Core™ Processors时会有Segmentation Fault Error

其他事项

  • Updated List of Capabilities Provided by JDK RPMs (JDK-8263575)

xml-commons-api, jaxp_parser_impl, 以及java-fonts已经从OracleJDK/OracleJRE RPMs移除

  • New Implementation of java.nio.channels.Selector on Microsoft Windows (JDK-8266369)

对windows的java.nio.channels.Selector API采用了更具可扩展的方式实现,原来的实现没有被移除,可以使用-Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider来继续使用

  • Parallel GC Enables Adaptive Parallel Reference Processing by Default (JDK-8204686)

针对Parallel GC默认开启了-XX:ParallelRefProcEnabled

  • URLClassLoader No Longer Throws Undocumented IllegalArgumentException From getResources and findResources (JDK-8262277)

URLClassLoader的getResources、findResources不再抛出文档未定义的IllegalArgumentException

小结

Java17主要有如下几个特性

doc