Java 14语言新特性
更新时间 2021-07-08 14:50:19    浏览 0   

TIP

本文主要是介绍 Java 14语言新特性 。

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

# 版本号

java -version
openjdk version "14" 2020-03-17
OpenJDK Runtime Environment (build 14+36-1461)
OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

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

# 特性列表

# 305:Pattern Matching for instanceof (Preview) (opens new window)

JDK14引入了preview版本的针对instanceof的模式匹配,其用法示例如下

    public boolean isBadRequestError(Exception ex) {
        return (ex instanceof HttpClientErrorException rce) &&
                HttpStatus.BAD_REQUEST == rce.getStatusCode();
    }

无需在自己进行类型强转

# 343:Packaging Tool (Incubator) (opens new window)

JDK14引入了jdk.incubator.jpackage.jmod,它基于JavaFX javapackager tool构建,目的在于创建一个简单的打包工具,可以用于构建exe、pkg、dmg、deb、rpm格式的安装文件;非模块化的app的构建示例如下

jpackage --name myapp --input lib --main-jar main.jar

# 345:NUMA-Aware Memory Allocation for G1 (opens new window)

实现了NUMA-aware的内存分配,以提升G1在大型机器上的性能

# 349:JFR Event Streaming (opens new window)

JDK11引入了JFR,使用的时候先dump到磁盘上然后再分析;而在JDK14则支持stream方式来进行持续性的监控,示例如下

public class AgentMain implements Runnable {

    public static void premain(String agentArgs, Instrumentation inst) {
        try {
            Logger.getLogger("AgentMain").log(
                    Level.INFO, "Attaching JFR Monitor");
            new Thread(new AgentMain()).start();
        } catch (Throwable t) {
            Logger.getLogger("AgentMain").log(
                    Level.SEVERE,"Unable to attach JFR Monitor", t);
        }
    }

    @Override
    public void run() {
        var sender = new JfrStreamEventSender();
        try (var rs = new RecordingStream()) {
            rs.enable("jdk.CPULoad")
                    .withPeriod(Duration.ofSeconds(1));
            rs.enable("jdk.JavaMonitorEnter")
                    .withThreshold(Duration.ofMillis(10));
            rs.onEvent("jdk.CPULoad", sender);
            rs.onEvent("jdk.JavaMonitorEnter", sender);
            rs.start();
        }
    }
}

# 352:Non-Volatile Mapped Byte Buffers (opens new window)

该特性新增了java.base/jdk/internal/misc/ExtendedMapMode.java以支持MappedByteBuffer访问non-volatile memory (NVM)

# 358:Helpful NullPointerExceptions (opens new window)

该特性可以更好地提示哪个地方出现的空指针,需要通过-XX:+ShowCodeDetailsInExceptionMessages开启,示例如下

public class NullPointerExample {

    public record City(String name){

    }

    public record Location(City city) {

    }

    public record User(String name, Location location) {

    }

    public static void main(String[] args){
        User user = new User("hello", new Location(null));
        System.out.println(user.location().city().name());
    }
}

输出如下

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.example.NullPointerExample$City.name()" because the return value of "com.example.NullPointerExample$Location.city()" is null
    at com.example.NullPointerExample.main(NullPointerExample.java:25)

# 359:Records (Preview) (opens new window)

JDK14引入了preview版本的record类型,示例如下

public record Point(int x, int y) {

    public Point {
        System.out.println(String.format("(%d,%d)", x, y));
    }

    public Point(int value) {
        this(value, value);
    }

    public static Point of(int value) {
        return new Point(value, value);
    }
}

javap反编译如下

javac --enable-preview -source 14 Point.java
javap -verbose Point.class
Compiled from "Point.java"
public final class com.example.domain.Point extends java.lang.Record {
  public com.example.domain.Point(int, int);
  public static com.example.domain.Point of(int);
  public java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
  public int x();
  public int y();
}

可以看见Point继承了java.lang.Record,而且通过invokedynamic提供了final的hashCode及equals

# 361:Switch Expressions (Standard) (opens new window)

JDK12引入的switch在JDK14变为正式版本,正式版主要是用->来替代以前的:+break;另外就是提供了yield来在block中返回值,示例如下

    public void testSwitchWithArrowBlockAndYield() {        int n = 3;        String quantityString = switch (n) {            case 1 -> "one";            case 2 -> "two";            default -> {                System.out.println("default");                yield "many";            }        };        System.out.println(quantityString);    }

# 362:Deprecate the Solaris and SPARC Ports (opens new window)

废弃了 Solaris/SPARC, Solaris/x64, and Linux/SPARC ports,以在未来的版本中移除

# 363:Remove the Concurrent Mark Sweep (CMS) Garbage Collector (opens new window)

移除了CMS垃圾收集器,如果在JDK14中使用-XX:+UseConcMarkSweepGC的话,会出现warning,但是不会exit而是以默认的垃圾收集器运行,如下

OpenJDK 64-Bit Server VM warning: Ignoring option UseConcMarkSweepGC; support was removed in 14.0

# 364:ZGC on macOS (opens new window)

之前的ZGC只能在linux上使用,现在mac上也能使用ZGC了,示例如下

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

# 365:ZGC on Windows (opens new window)

之前的ZGC只能在linux上使用,现在windows(不能低于1803版本)上也能使用ZGC了,示例如下

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

# 366:Deprecate the ParallelScavenge + SerialOld GC Combination (opens new window)

废弃了parallel young generation GC与SerialOld GC的组合(-XX:+UseParallelGC与-XX:-UseParallelOldGC配合开启),现在使用-XX:+UseParallelGC -XX:-UseParallelOldGC或者-XX:-UseParallelOldGC都会出现告警如下

OpenJDK 64-Bit Server VM warning: Option UseParallelOldGC was deprecated in version 14.0 and will likely be removed in a future release.

单独使用-XX:+UseParallelGC则开启parallel young and old generation GC algorithms

# 367:Remove the Pack200 Tools and API (opens new window)

移除了Pack200 API

# 368:Text Blocks (Second Preview) (opens new window)

JDK13引入的text blocks进行第二轮preview,JDK14的版本主要增加了两个escape sequences,分别是\<line-terminator>\s escape sequence,示例如下

    @Test    public void testTextBlockWithTwoNewEscapeSequences() {        String inOneLine = """                Lorem ipsum dolor sit amet, consectetur adipiscing \                elit, sed do eiusmod tempor incididunt ut labore \                et dolore magna aliqua.\                """;        System.out.println(inOneLine);        String singleSpace = """                red  \s                green\s                blue \s                """;        System.out.println(singleSpace);    }

# 370:Foreign-Memory Access API (Incubator) (opens new window)

提供了incubator版本的API用于操纵堆外内存,使用示例如下

    @Test    public void testForeignMemoryAccessAPI() {        SequenceLayout intArrayLayout                = MemoryLayout.ofSequence(25,                MemoryLayout.ofValueBits(32,                        ByteOrder.nativeOrder()));        VarHandle intElemHandle                = intArrayLayout.varHandle(int.class,                MemoryLayout.PathElement.sequenceElement());        try (MemorySegment segment = MemorySegment.allocateNative(intArrayLayout)) {            MemoryAddress base = segment.baseAddress();            for (int i = 0; i < intArrayLayout.elementCount().getAsLong(); i++) {                intElemHandle.set(base, (long) i, i);            }        }    }

# 细项解读

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

# 添加项

给SAX ContentHandler新增了方法,如下

    default void declaration(String version, String encoding, String standalone)        throws SAXException    {        //no op    }

# 移除项

移除了sun.nio.cs.map属性

移除了java.security.acl

移除了默认的keytool -keyalg

# 废弃项

废弃了Thread的如下方法

Thread.suspend()
Thread.resume()
ThreadGroup.suspend()
ThreadGroup.resume()
ThreadGroup.allowThreadSuspension(boolean)

# 已知问题

已知在macOS上的Dark Mode有Text Visibility Issues

# 其他事项

Thread.countStackFrames开启转为无条件地抛出UnsupportedOperationException

# 参考文章

  • https://segmentfault.com/a/1190000022064656
更新时间: 2021-07-08 14:50:19
  0
手机看
公众号
讨论
左栏
全屏
上一篇
下一篇
扫一扫 手机阅读
可分享给好友和朋友圈