Java16的新特性

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

版本号

java -version
openjdk version "16" 2021-03-16
OpenJDK Runtime Environment (build 16+36-2231)
OpenJDK 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)

从version信息可以看出是build 16+36

特性列表

JEP 338: Vector API (Incubator)

提供了jdk.incubator.vector来用于矢量计算,实例如下

static final VectorSpecies SPECIES = FloatVector.SPECIES_256;

void vectorComputation(float[] a, float[] b, float[] c) {

    for (int i = 0; i < a.length; i += SPECIES.length()) {
        var m = SPECIES.indexInRange(i, a.length);
        // FloatVector va, vb, vc;
        var va = FloatVector.fromArray(SPECIES, a, i, m);
        var vb = FloatVector.fromArray(SPECIES, b, i, m);
        var vc = va.mul(va).
                    add(vb.mul(vb)).
                    neg();
        vc.intoArray(c, i, m);
    }
}

JEP 347: Enable C++14 Language Features

在JDK C++的源码中允许使用C++14的语言特性

JEP 357: Migrate from Mercurial to Git

OpenJDK源码的版本控制从Mercurial (hg) 迁移到git

JEP 369: Migrate to GitHub

将OpenJDK源码的版本控制迁移到github上

JEP 376: ZGC: Concurrent Thread-Stack Processing

实现了并发thread-stack处理来降低GC safepoints的负担

JEP 380: Unix-Domain Socket Channels

对socket channel及server socket channel的api提供对unix domain socket的支持

JEP 386: Alpine Linux Port

将glibc的jdk移植到使用musl的alpine linux上

JEP 387: Elastic Metaspace

支持不再使用的class metadata归还内存给操作系统,降低内存占用

JEP 388: Windows/AArch64 Port

移植JDK到Windows/AArch64

JEP 389: Foreign Linker API (Incubator)

提供jdk.incubator.foreign来简化native code的调用

JEP 390: Warnings for Value-Based Classes

提供@jdk.internal.ValueBased来用于标注作为value-based的类,实例如下

@jdk.internal.ValueBased
public final class SomeVbc {

    public SomeVbc() {}

    final String ref = "String";

    void abuseVbc() {

        synchronized(ref) {           // OK
            synchronized (this) {     // WARN
            }
        }
    }
}

final class AuxilliaryAbuseOfVbc {

    void abuseVbc(SomeVbc vbc) {

        synchronized(this) {           // OK
            synchronized (vbc) {       // WARN
            }
        }
    }
}

JEP 392: Packaging Tool

jpackage在JDK14引入,JDK15作为incubating工具,在JDK16转正,从jdk.incubator.jpackage转为jdk.jpackage。它支持Linux: deb and rpm、macOS: pkg and dmg、Windows: msi and exe

JEP 393: Foreign-Memory Access API (Third Incubator)

Foreign-Memory Access API在JDK14首次引入作为incubating API,在JDK15处于第二轮incubating,在JDK16处于第三轮incubating

JEP 394: Pattern Matching for instanceof

instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16转正

JEP 395: Records

Record类型在JDK14作为preview,在JDK15处于第二轮preview,在JDK16转正

JEP 396: Strongly Encapsulate JDK Internals by Default

对内部的api进行更多的封装,鼓励开发者从使用内部的方法迁移到标准的API,但是sun.misc.Unsafe还是继续保留

JEP 397: Sealed Classes (Second Preview)

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

细项解读

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

添加项

  • Add InvocationHandler::invokeDefault Method for Proxy's Default Method Support (JDK-8159746)

给InvocationHandler添加invokeDefault方法

  • Day Period Support Added to java.time Formats (JDK-8247781)

java.time支持Day Period

  • Add Stream.toList() Method (JDK-8180352) (JDK-8180352)

Stream新增toList方法

  • Concurrently Uncommit Memory in G1 (JDK-8236926)

针对G1提供了并发归还内存给操作系统

移除项

  • Removal of Experimental Features AOT and Graal JIT (JDK-8255616)

移除jaotc工具

  • Deprecated Tracing Flags Are Obsolete and Must Be Replaced With Unified Logging Equivalents (JDK-8256718)

使用-Xlog:class+load=info替代-XX:+TraceClassLoading;使用-Xlog:class+unload=info替代-XX:+TraceClassUnloading;使用-Xlog:exceptions=info替代-XX:+TraceExceptions

废弃项

  • Terminally Deprecated ThreadGroup stop, destroy, isDestroyed, setDaemon and isDaemon (JDK-8256643)

废弃ThreadGroup的stop, destroy, isDestroyed, setDaemon, isDaemon方法

  • Deprecated the java.security.cert APIs That Represent DNs as Principal or String Objects (JDK-8241003)

废弃了java.security.cert.X509Certificate的getIssuerDN()、getSubjectDN()方法

已知问题

  • Incomplete Support for Unix Domain Sockets in Windows 2019 Server (JDK-8259014)

Unix Domain Sockets对Windows 2019 Server的支持还不完善

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

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

其他事项

  • Enhanced Support of Proxy Class (JDK-8236862)

对Proxy Class进行了增强,支持jdk.serialProxyInterfaceLimit属性

  • Support Supplementary Characters in String Case Insensitive Operations (JDK-8248655)

对compareToIgnoreCase、equalsIgnoreCase、regionMatches方法的Case Insensitive语义进行了增强

  • HttpClient.newHttpClient and HttpClient.Builder.build Might Throw UncheckedIOException (JDK-8248006)

HttpClient.newHttpClient及HttpClient.Builder.build方法可能抛出UncheckedIOException

  • The Default HttpClient Implementation Returns Cancelable Futures (JDK-8245462)

默认的HttpClient实现返回Cancelable Futures

小结

Java16主要有如下几个特性

  • JEP 338: Vector API (Incubator)
  • JEP 347: Enable C++14 Language Features
  • JEP 357: Migrate from Mercurial to Git
  • JEP 369: Migrate to GitHub
  • JEP 376: ZGC: Concurrent Thread-Stack Processing
  • JEP 380: Unix-Domain Socket Channels
  • JEP 386: Alpine Linux Port
  • JEP 387: Elastic Metaspace
  • JEP 388: Windows/AArch64 Port
  • JEP 389: Foreign Linker API (Incubator)
  • JEP 390: Warnings for Value-Based Classes
  • JEP 392: Packaging Tool
  • JEP 393: Foreign-Memory Access API (Third Incubator)
  • JEP 394: Pattern Matching for instanceof
  • JEP 395: Records
  • JEP 396: Strongly Encapsulate JDK Internals by Default
  • JEP 397: Sealed Classes (Second Preview)

doc

  • JDK 16 Features
  • JDK 16 Release Notes
  • Oracle JDK 16 Release Notes
  • Java SE deprecated-list
  • The Arrival of Java 16
  • Java 16 and IntelliJ IDEA

你可能感兴趣的:(Java16的新特性)