正文
▐
3.
Feature:
Unnamed Patterns and Variables (Previe
w) & Record Patterns
(这两个feature比较关联的,就一起概述了)
动机:提升可读性,并且让程序员可以清楚知道哪些成员变量被使用,哪些成员变量未被使用,preview的功能就是可以使用下划线来代替不想使用到的record里面的成员。
package org.example;
import java.util.List;
public class UnnamedPatternsAndVariables {
public static void main(String[] args) {
process(List.of(new SealedClassA("A"), new SealedClassB("B", 123), new SealedClassC("C", 321, "hidden hobby")));
}
public static void process(List list) {
for (SealedInterface sealedInterface : list) {
if (sealedInterface instanceof SealedClassA(String name)) {
System.out.println("The name of SealedClassA is " + name);
} else if (sealedInterface instanceof SealedClassB(String name, Integer age)) {
System.out.println("The name of SealedClassB is " + name + " and age is " + age);
} else if (sealedInterface instanceof SealedClassC(String name, Integer age, String _)) {
System.out.println("The name of SealedClassC is " + name + " and age is " + age + " and hobby is hidden");
}
}
}
public sealed interface SealedInterface permits SealedClassA, SealedClassB, SealedClassC {
}
public record SealedClassA(String name) implements SealedInterface {
}
public record SealedClassB(String name, Integer age) implements SealedInterface {
}
public record SealedClassC(String name, Integer age, String hobby) implements SealedInterface {
}
}
▐
4.
Feature
: Scoped Values (Preview)
特性目标:
易用性:提供一种编程模型,以便在一个线程内和子线程之间共享数据,从而简化对数据流的推理。
可理解性:使共享数据的生命周期能够从代码的语法结构中可见。
稳健性:确保调用者共享的数据只能被合法的被调用者检索。
性能:允许共享数据是不可变的,以便于被大量线程共享,并支持运行时优化。
可以重新绑定,面向之前通过上下文context传递的场景,以及使用ThreadLocal的场景;如果在中途使用异步线程进行额外操作处理,这里的值绑定会丢失,需要显示的在线程之间做传递。
package org.example;
import java.lang.ScopedValue;
public class ScopedValues {
public static void main(String[] args) {
ScopedValue.runWhere(EagleEye.TRACE_ID, "123456", () -> {
RPCProcess rpcProcess = new RPCProcess();
rpcProcess.process();
});
}
}
class EagleEye {
public static final ScopedValue TRACE_ID = ScopedValue.newInstance();
}
class RPCProcess {
public void process() {
System.out.println(STR."TRACE_ID: \{EagleEye.TRACE_ID.get()}, do rpc process");
ServerService serverService = new ServerService();
serverService.bizProcess();
}
}
class ServerService {
public void bizProcess() {
System.out.println(STR."TRACE_ID: \{EagleEye.TRACE_ID.get()}, do biz process");
AsyncProcessor asyncProcessor = new AsyncProcessor();
asyncProcessor.asyncProc();
}
}
class AsyncProcessor {
public void asyncProc() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(STR."TRACE_ID: \{!EagleEye.TRACE_ID.isBound() ? null : EagleEye.TRACE_ID.get()}, do async process");
}
});
thread.start();
}
}
▐
5.
Feature
: Foreign Function & Memory API (Third Preview)
动机:为了Java程序员提供更可靠的操作native代码和存储(操作系统层面)的API能力。
JDK19的时候首次提出,JDK20第二版preivew,JDK21第三版preview;该特性主要是为了提供一种更高效、便捷和安全的方式,去调用系统底层的类库,但是难点个人感觉还是在调用安全性上,这类工具在生产环境中的应用都需要比较慎重,因为都是属于运行时的错误,而且不容易测试发现。
package org.example;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
public class ForeignFunctionMemory {
public static void main(String[] args) {
MemorySegment memorySegment = Arena.ofAuto().allocate(1024);
memorySegment.set(ValueLayout.JAVA_BYTE, 0, (byte) 1);
byte target = memorySegment.get(ValueLayout.JAVA_BYTE, 0);
System.out.println(target);
}
}
▐