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
恢复始终执行严格模式的浮点定义,修复25年前英特尔的浮点指令存在的一些问题
引入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();
使用Apple Metal API为macOS新增了Java 2D internal rendering pipeline
迁移JDK到macOS/AArch64
标记Applet API为废弃方便后续移除,具体如下
java.applet.Applet
java.applet.AppletStub
java.applet.AppletContext
java.applet.AudioClip
javax.swing.JApplet
java.beans.AppletInitializer
对JDK内部的api进行更强的封装,是JEP 396: Strongly Encapsulate JDK Internals by Default的后续
引入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();
};
}
移除Remote Method Invocation (RMI),它在JDK15的JEP 385被废弃
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 { ... }
移除实验性的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
废弃java1.0引入的Security Manager方便后续移除
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
JDK16引入了JEP 338: Vector API (Incubator)提供了jdk.incubator.vector来用于矢量计算,JDK17进行改进并作为第二轮的incubator
允许应用去配置指定上下文及动态选择的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,这里举几个例子。
更新了java.net.DatagramSocket用于支持joining multicast groups
java.io.Console新增了方法用于返回console的charset
JDK Flight Recorder新增了jfr.Deserialization实现
引入了
-Xlog:async
参数用于异步日志,以及-XX:AsyncLogBufferSize=<bytes>
用于控制buffer的大小
移除sun.misc.Unsafe::defineAnonymousClass方法
废弃了Kerberos的des3-hmac-sha1及rc4-hmac这两个encryption types
废弃了如下几个工厂
static void ServerSocket.setSocketFactory(SocketImplFactory fac)
static void Socket.setSocketImplFactory(SocketImplFactory fac)
static void DatagramSocket.setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
TreeMap.computeIfAbsent方法针对null的处理与规范有偏差
当运行在9th and 10th Generation Intel® Core™ Processors时会有Segmentation Fault Error
xml-commons-api, jaxp_parser_impl, 以及java-fonts已经从OracleJDK/OracleJRE RPMs移除
对windows的java.nio.channels.Selector API采用了更具可扩展的方式实现,原来的实现没有被移除,可以使用
-Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider
来继续使用
针对Parallel GC默认开启了
-XX:ParallelRefProcEnabled
URLClassLoader的getResources、findResources不再抛出文档未定义的IllegalArgumentException
Java17主要有如下几个特性