Java21的新特性

發表於2023-09-27
Java語言特性系列

本文主要講述一下Java21的新特性

版本號

java -version
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)
從version資訊可以看出是build 21+35

特性列表

JEP 430: String Templates (Preview)

在java21之前,字串拼接或者字串與表示式組合主要是用StringBuilder、String::format、java.text.MessageFormat,不過可讀性都不是太好,java21引入了StringTemplate(java.lang.StringTemplate)來解決這個問題。

@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
public interface StringTemplate {
    List<String> fragments();
    List<Object> values();
    default String interpolate() {
        return StringTemplate.interpolate(fragments(), values());
    }
    default <R, E extends Throwable> R
    process(Processor<? extends R, ? extends E> processor) throws E {
        Objects.requireNonNull(processor, "processor should not be null");

        return processor.process(this);
    }

    static String interpolate(List<String> fragments, List<?> values) {
        Objects.requireNonNull(fragments, "fragments must not be null");
        Objects.requireNonNull(values, "values must not be null");
        int fragmentsSize = fragments.size();
        int valuesSize = values.size();
        if (fragmentsSize != valuesSize + 1) {
            throw new IllegalArgumentException("fragments must have one more element than values");
        }
        JavaTemplateAccess JTA = SharedSecrets.getJavaTemplateAccess();
        return JTA.interpolate(fragments, values);
    }

    Processor<String, RuntimeException> STR = StringTemplate::interpolate;
    Processor<StringTemplate, RuntimeException> RAW = st -> st;

    @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
    @FunctionalInterface
    public interface Processor<R, E extends Throwable> {
        R process(StringTemplate stringTemplate) throws E;
        static <T> Processor<T, RuntimeException> of(Function<? super StringTemplate, ? extends T> process) {
            return process::apply;
        }

        @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
        public sealed interface Linkage permits FormatProcessor {
            MethodHandle linkage(List<String> fragments, MethodType type);
        }
    }
}

StringTemplate是個介面,它定義了fragments、values、interpolate、process方法,同時提供了interpolate、process方法的預設實現;同時內建了兩個processor,分別是STR和RAW,他們的區別在於RAW可以獲取到StringTemplate型別,STR則是StringTemplate執行了interpolate方法之後的結果,獲得到的是最終結果String;其基本語法就是用\{}來包含變數或者表示式

RAW示例

    @Test
    public void testRaw() {
        int x = 10;
        int y = 20;
        StringTemplate st = RAW."\{x} + \{y} = \{x + y}";
        List<String> fragments = st.fragments();
        List<Object> values = st.values();
        log.info("fragments:{}, values:{}, st:{}", fragments, values, st.interpolate());
    }
輸出fragments:[, + , = , ], values:[10, 20, 30], st:10 + 20 = 30
STR示例
    @Test
    public void testStr() {
        String name = "Joan";
        String info = STR."My name is \{name}";
        System.out.println(info);
    }

輸出My name is Joan

也支援方法呼叫和表示式

    @Test
    public void testStrExpression() {
        String filePath = "tmp.dat";
        File file = new File(filePath);
        String msg = STR. "The file \{ filePath } \{ file.exists() ? "does" : "does not" } exist" ;
        System.out.println(msg);
    }
最後輸出The file tmp.dat does not exist

對於還有格式化需求的,提供了java.util.FMT

    @Test
    public void testFmt() {
        record Rectangle(String name, double width, double height) {
            double area() {
                return width * height;
            }
        }
        Rectangle[] zone = new Rectangle[] {
                new Rectangle("Alfa", 17.8, 31.4),
                new Rectangle("Bravo", 9.6, 12.4),
                new Rectangle("Charlie", 7.1, 11.23),
        };
        String table = FMT."""
    Description     Width    Height     Area
    %-12s\{zone[0].name}  %7.2f\{zone[0].width}  %7.2f\{zone[0].height}     %7.2f\{zone[0].area()}
    %-12s\{zone[1].name}  %7.2f\{zone[1].width}  %7.2f\{zone[1].height}     %7.2f\{zone[1].area()}
    %-12s\{zone[2].name}  %7.2f\{zone[2].width}  %7.2f\{zone[2].height}     %7.2f\{zone[2].area()}
    \{" ".repeat(28)} Total %7.2f\{zone[0].area() + zone[1].area() + zone[2].area()}
    """;
        System.out.println(table);
    }

也可以自定義processor

@Test
    public void testCustomProcessor() {
        var MYJSON = StringTemplate.Processor.of(
                (StringTemplate st) -> com.alibaba.fastjson.JSON.parseObject(st.interpolate())
        );

        String name    = "Joan Smith";
        String phone   = "555-123-4567";
        String address = "1 Maple Drive, Anytown";
        JSONObject doc = MYJSON."""
    {
        "name":    "\{name}",
        "phone":   "\{phone}",
        "address": "\{address}"
    }
    """;
        System.out.println(doc);
    }

JEP 431: Sequenced Collections

java21引入了java.util.SequencedCollection、java.util.SequencedMap來統一各類集合的順序方法方法

public interface SequencedCollection<E> extends Collection<E> {
    SequencedCollection<E> reversed();
    default void addFirst(E e) {
        throw new UnsupportedOperationException();
    }
    default void addLast(E e) {
        throw new UnsupportedOperationException();
    }
    default E getFirst() {
        return this.iterator().next();
    }
    default E getLast() {
        return this.reversed().iterator().next();
    }
    default E removeFirst() {
        var it = this.iterator();
        E e = it.next();
        it.remove();
        return e;
    }
    default E removeLast() {
        var it = this.reversed().iterator();
        E e = it.next();
        it.remove();
        return e;
    }
}
SequencedCollection繼承了Collection介面,同時定義了reversed,提供了addFirst、addLast、getFirst、getLast、removeFirst、removeLast的default實現;List、SequencedSet介面都繼承了SequencedCollection介面
public interface SequencedMap<K, V> extends Map<K, V> {
    SequencedMap<K, V> reversed();
    default Map.Entry<K,V> firstEntry() {
        var it = entrySet().iterator();
        return it.hasNext() ? new NullableKeyValueHolder<>(it.next()) : null;
    }
    default Map.Entry<K,V> lastEntry() {
        var it = reversed().entrySet().iterator();
        return it.hasNext() ? new NullableKeyValueHolder<>(it.next()) : null;
    }
    default Map.Entry<K,V> pollFirstEntry() {
        var it = entrySet().iterator();
        if (it.hasNext()) {
            var entry = new NullableKeyValueHolder<>(it.next());
            it.remove();
            return entry;
        } else {
            return null;
        }
    }
    default Map.Entry<K,V> pollLastEntry() {
        var it = reversed().entrySet().iterator();
        if (it.hasNext()) {
            var entry = new NullableKeyValueHolder<>(it.next());
            it.remove();
            return entry;
        } else {
            return null;
        }
    }
    default V putFirst(K k, V v) {
        throw new UnsupportedOperationException();
    }
    default V putLast(K k, V v) {
        throw new UnsupportedOperationException();
    }
    default SequencedSet<K> sequencedKeySet() {
        class SeqKeySet extends AbstractMap.ViewCollection<K> implements SequencedSet<K> {
            Collection<K> view() {
                return SequencedMap.this.keySet();
            }
            public SequencedSet<K> reversed() {
                return SequencedMap.this.reversed().sequencedKeySet();
            }
            public boolean equals(Object other) {
                return view().equals(other);
            }
            public int hashCode() {
                return view().hashCode();
            }
        }
        return new SeqKeySet();
    }
    default SequencedCollection<V> sequencedValues() {
        class SeqValues extends AbstractMap.ViewCollection<V> implements SequencedCollection<V> {
            Collection<V> view() {
                return SequencedMap.this.values();
            }
            public SequencedCollection<V> reversed() {
                return SequencedMap.this.reversed().sequencedValues();
            }
        }
        return new SeqValues();
    }
    default SequencedSet<Map.Entry<K, V>> sequencedEntrySet() {
        class SeqEntrySet extends AbstractMap.ViewCollection<Map.Entry<K, V>>
                implements SequencedSet<Map.Entry<K, V>> {
            Collection<Map.Entry<K, V>> view() {
                return SequencedMap.this.entrySet();
            }
            public SequencedSet<Map.Entry<K, V>> reversed() {
                return SequencedMap.this.reversed().sequencedEntrySet();
            }
            public boolean equals(Object other) {
                return view().equals(other);
            }
            public int hashCode() {
                return view().hashCode();
            }
        }
        return new SeqEntrySet();
    }              
}
SequencedMap介面繼承了Map介面,它定義了reversed方法,同時提供了firstEntry、lastEntry、pollFirstEntry、pollLastEntry、putFirst、putLast、sequencedKeySet、sequencedValues、sequencedEntrySet方法的預設實現

此次版本的變動:
SequencedCollection

  • List現在有作為其直接的超級介面,SequencedCollection
  • Deque現在有作為其直接的超級介面,SequencedCollection
  • LinkedHashSet另外實現SequencedSet介面
  • SortedSet現在有作為其直接的超級介面,SequencedSet
  • LinkedHashMap另外實現SequencedMap介面
  • SortedMap現在有作為它的直接超級介面,SequencedMap

另外Collections還提供了工廠方法用於返回不可變型別

Collections.unmodifiableSequencedCollection(sequencedCollection)
Collections.unmodifiableSequencedSet(sequencedSet)
Collections.unmodifiableSequencedMap(sequencedMap)

JEP 439: Generational ZGC

ZGC分代回收無疑是一個重磅的GC特性,ZGC之前的版本不支援分代回收,此次支援分代回收的話,可以更方便地對年輕代進行收集,提高GC效能。目前是分代與非分代都支援,使用分代則透過-XX:+UseZGC-XX:+ZGenerational開啟,後續版本將會把分代設定為預設的,而-XX:-ZGenerational用於開啟非分代,最後將會廢除非分代的支援,屆時ZGenerational引數也就沒有作用了。

JEP 440: Record Patterns

JDK19的JEP 405: Record Patterns (Preview)將Record的模式匹配作為第一次preview
JDK20的JEP 432: Record Patterns (Second Preview)作為第二次preview
此次在JDK21則作為正式版本釋出,使用示例如下
record Point(int x, int y) {}

// As of Java 21
static void printSum(Object obj) {
    if (obj instanceof Point(int x, int y)) {
        System.out.println(x+y);
    }
}

enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}
// As of Java 21
static void printUpperLeftColoredPoint(Rectangle r) {
    if (r instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)) {
         System.out.println(ul.c());
    }
}

static void printColorOfUpperLeftPoint(Rectangle r) {
    if (r instanceof Rectangle(ColoredPoint(Point p, Color c),
                               ColoredPoint lr)) {
        System.out.println(c);
    }
}

JEP 441: Pattern Matching for switch

在JDK14JEP 305: Pattern Matching for instanceof (Preview)作為preview
在JDK15JEP 375: Pattern Matching for instanceof (Second Preview)作為第二輪的preview
在JDK16JEP 394: Pattern Matching for instanceof轉正
JDK17引入JEP 406: Pattern Matching for switch (Preview)
JDK18的JEP 420: Pattern Matching for switch (Second Preview)則作為第二輪preview
JDK19的JEP 427: Pattern Matching for switch (Third Preview)作為第三輪preview
JDK20的JEP 433: Pattern Matching for switch (Fourth Preview)作為第四輪preview
而此次JDK21將Pattern Matching for switch作為正式版本釋出,示例如下
// Prior to Java 21
static String formatter(Object obj) {
    String formatted = "unknown";
    if (obj instanceof Integer i) {
        formatted = String.format("int %d", i);
    } else if (obj instanceof Long l) {
        formatted = String.format("long %d", l);
    } else if (obj instanceof Double d) {
        formatted = String.format("double %f", d);
    } else if (obj instanceof String s) {
        formatted = String.format("String %s", s);
    }
    return formatted;
}

// As of Java 21
static String formatterPatternSwitch(Object obj) {
    return switch (obj) {
        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        -> obj.toString();
    };
}

// As of Java 21
static void testFooBarNew(String s) {
    switch (s) {
        case null         -> System.out.println("Oops");
        case "Foo", "Bar" -> System.out.println("Great");
        default           -> System.out.println("Ok");
    }
}

// As of Java 21
static void testStringEnhanced(String response) {
    switch (response) {
        case null -> { }
        case "y", "Y" -> {
            System.out.println("You got it");
        }
        case "n", "N" -> {
            System.out.println("Shame");
        }
        case String s
        when s.equalsIgnoreCase("YES") -> {
            System.out.println("You got it");
        }
        case String s
        when s.equalsIgnoreCase("NO") -> {
            System.out.println("Shame");
        }
        case String s -> {
            System.out.println("Sorry?");
        }
    }
}

// As of Java 21
static void exhaustiveSwitchWithBetterEnumSupport(CardClassification c) {
    switch (c) {
        case Suit.CLUBS -> {
            System.out.println("It's clubs");
        }
        case Suit.DIAMONDS -> {
            System.out.println("It's diamonds");
        }
        case Suit.HEARTS -> {
            System.out.println("It's hearts");
        }
        case Suit.SPADES -> {
            System.out.println("It's spades");
        }
        case Tarot t -> {
            System.out.println("It's a tarot");
        }
    }
}

// As of Java 21
sealed interface Currency permits Coin {}
enum Coin implements Currency { HEADS, TAILS } 

static void goodEnumSwitch1(Currency c) {
    switch (c) {
        case Coin.HEADS -> {    // Qualified name of enum constant as a label
            System.out.println("Heads");
        }
        case Coin.TAILS -> {
            System.out.println("Tails");
        }
    }
}

static void goodEnumSwitch2(Coin c) {
    switch (c) {
        case HEADS -> {
            System.out.println("Heads");
        }
        case Coin.TAILS -> {    // Unnecessary qualification but allowed
            System.out.println("Tails");
        }
    }
}

// As of Java 21
static void testNew(Object obj) {
    switch (obj) {
        case String s when s.length() == 1 -> ...
        case String s                      -> ...
        ...
    }
}

JEP 442: Foreign Function & Memory API (Third Preview)

Foreign Function & Memory (FFM) API包含了兩個incubating API
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 (JEP 389)
FFM API在JDK 17的JEP 412: Foreign Function & Memory API (Incubator)作為incubator引入
FFM API在JDK 18的JEP 419: Foreign Function & Memory API (Second Incubator)作為第二輪incubator
JDK19的JEP 424: Foreign Function & Memory API (Preview)則將FFM API作為preview API
JDK20的JEP 434: Foreign Function & Memory API (Second Preview)作為第二輪preview
JDK21則作為第三輪的preview,使用示例
.javac --release 21 --enable-preview ...java --enable-preview ...

// 1. Find foreign function on the C library path
Linker linker          = Linker.nativeLinker();
SymbolLookup stdlib    = linker.defaultLookup();
MethodHandle radixsort = linker.downcallHandle(stdlib.find("radixsort"), ...);
// 2. Allocate on-heap memory to store four strings
String[] javaStrings = { "mouse", "cat", "dog", "car" };
// 3. Use try-with-resources to manage the lifetime of off-heap memory
try (Arena offHeap = Arena.ofConfined()) {
    // 4. Allocate a region of off-heap memory to store four pointers
    MemorySegment pointers
        = offHeap.allocateArray(ValueLayout.ADDRESS, javaStrings.length);
    // 5. Copy the strings from on-heap to off-heap
    for (int i = 0; i < javaStrings.length; i++) {
        MemorySegment cString = offHeap.allocateUtf8String(javaStrings[i]);
        pointers.setAtIndex(ValueLayout.ADDRESS, i, cString);
    }
    // 6. Sort the off-heap data by calling the foreign function
    radixsort.invoke(pointers, javaStrings.length, MemorySegment.NULL, '\0');
    // 7. Copy the (reordered) strings from off-heap to on-heap
    for (int i = 0; i < javaStrings.length; i++) {
        MemorySegment cString = pointers.getAtIndex(ValueLayout.ADDRESS, i);
        javaStrings[i] = cString.getUtf8String(0);
    }
} // 8. All off-heap memory is deallocated here
assert Arrays.equals(javaStrings,
                     new String[] {"car", "cat", "dog", "mouse"});  // true

JEP 443: Unnamed Patterns and Variables (Preview)

Unnamed Patterns and Variables支援用_來替代沒有使用的變數宣告,比如

r instanceof Point _
r instanceof ColoredPoint(Point(int x, int _), Color _)
if (r instanceof ColoredPoint(_, Color c)) { ... c ... }
switch (b) {
    case Box(RedBall _), Box(BlueBall _) -> processBox(b);
    case Box(GreenBall _)                -> stopProcessing();
    case Box(_)                          -> pickAnotherBox();
}
int acc = 0;
for (Order _ : orders) {
    if (acc < LIMIT) { 
        ... acc++ ...
    }
}
while (q.size() >= 3) {
    var x = q.remove();
    var _ = q.remove();
    var _ = q.remove(); 
    ... new Point(x, 0) ...
}

JEP 444: Virtual Threads

在JDK19https://openjdk.org/jeps/425)作為第一次preview
在JDK20JEP 436: Virtual Threads (Second Preview)作為第二次preview,此版本java.lang.ThreadGroup被永久廢棄
在JDK21版本,Virtual Threads正式釋出,與之前版本相比,這次支援了threadlocal,然後也可以透過Thread.Builder來建立,而且也支援threaddump(jcmd <pid> Thread.dump_to_file -format=json <file>)

使用示例

void handle(Request request, Response response) {
    var url1 = ...
    var url2 = ...
 
    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
        var future1 = executor.submit(() -> fetchURL(url1));
        var future2 = executor.submit(() -> fetchURL(url2));
        response.send(future1.get() + future2.get());
    } catch (ExecutionException | InterruptedException e) {
        response.fail(e);
    }
}
 
String fetchURL(URL url) throws IOException {
    try (var in = url.openStream()) {
        return new String(in.readAllBytes(), StandardCharsets.UTF_8);
    }
}
一般用Executors.newVirtualThreadPerTaskExecutor()是想透過池化技術來減少物件建立開銷,不過由於虛擬執行緒相比平臺執行緒更為"廉價",因而不再需要池化,如果需要控制虛擬執行緒數則可以使用訊號量的方式,因而提供了Thread.Builder來直接建立虛擬執行緒,示例如下:
Thread thread = Thread.ofVirtual().name("duke").unstarted(runnable);
Thread.startVirtualThread(Runnable) 

JEP 445: Unnamed Classes and Instance Main Methods (Preview)

未命名的類和例項main方法這個特性可以簡化hello world示例,方便java新手入門,示例如下

        static void main(String[] args) {
            System.out.println("static main with args");
        }

        static void main() {
            System.out.println("static main without args");
        }

        void main(String[] args) {
            System.out.println("main with args");
        }

        void main() {
            System.out.println("main with without args");
        }
javac --release 21 --enable-preview Main.javajava --enable-preview Main
其中main方法選擇的優先順序是static的優於非static的,然後有args的優於沒有args的

JEP 446: Scoped Values (Preview)

Scoped Values在JDK20的JEP 429: Scoped Values (Incubator)作為Incubator
此次在JDK21作為preview版本
ScopedValue是一種類似ThreadLocal的執行緒內/父子執行緒傳遞變數的更優方案。ThreadLocal提供了一種無需在方法引數上傳遞通用變數的方法,InheritableThreadLocal使得子執行緒可以複製繼承父執行緒的變數。但是ThreadLocal提供了set方法,變數是可變的,另外remove方法很容易被忽略,導致線上程池場景下很容易造成記憶體洩露。ScopedValue則提供了一種不可變、不複製的方案,即不提供set方法,子執行緒不需要複製就可以訪問父執行緒的變數。具體使用如下:
class Server {
  public final static ScopedValue<User> LOGGED_IN_USER = ScopedValue.newInstance();
 
  private void serve(Request request) {
    // ...
    User loggedInUser = authenticateUser(request);
    ScopedValue.where(LOGGED_IN_USER, loggedInUser)
               .run(() -> restAdapter.processRequest(request));
    // ...
  }
}
透過ScopedValue.where可以繫結ScopedValue的值,然後在run方法裡可以使用,方法執行完畢自行釋放,可以被垃圾收集器回收

JEP 448: Vector API (Sixth Incubator)

JDK16引入了JEP 338: Vector API (Incubator)提供了jdk.incubator.vector來用於向量計算
JDK17進行改進並作為第二輪的incubatorJEP 414: Vector API (Second Incubator)
JDK18的JEP 417: Vector API (Third Incubator)進行改進並作為第三輪的incubator
JDK19的JEP 426:Vector API (Fourth Incubator)作為第四輪的incubator
JDK20的JEP 438: Vector API (Fifth Incubator)作為第五輪的incubator
而JDK21則作為第六輪的incubator,使用示例如下
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;

void vectorComputation(float[] a, float[] b, float[] c) {
    int i = 0;
    int upperBound = SPECIES.loopBound(a.length);
    for (; i < upperBound; i += SPECIES.length()) {
        // FloatVector va, vb, vc;
        var va = FloatVector.fromArray(SPECIES, a, i);
        var vb = FloatVector.fromArray(SPECIES, b, i);
        var vc = va.mul(va)
                   .add(vb.mul(vb))
                   .neg();
        vc.intoArray(c, i);
    }
    for (; i < a.length; i++) {
        c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f;
    }
}

JEP 449: Deprecate the Windows 32-bit x86 Port for Removal

廢棄了對Windows 32-bit x86 (x86-32)的移植,以便後續版本刪除

JEP 451: Prepare to Disallow the Dynamic Loading of Agents

對將代理動態載入到正在執行的 JVM 中時發出警告,後續版本將不允許動態載入agent。

在 JDK 9 及更高版本中,可以透過-XX:-EnableDynamicAgentLoading禁止動態載入agent。
在 JDK 21 中,允許動態載入agent,但 JVM 會在發生時發出警告。例如:
WARNING: A {Java,JVM TI} agent has been loaded dynamically (file:/u/bob/agent.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release

若要允許工具動態載入agent而不發出警告,使用者必須在命令列上使用-XX:+EnableDynamicAgentLoading

JEP 452: Key Encapsulation Mechanism API

Key Encapsulation Mechanism(KEM)是一種現代加密技術,它使用非對稱或公鑰加密來保護對稱金鑰。傳統的方法是使用公鑰加密一個隨機生成的對稱金鑰,但這需要填充,並且可能難以證明安全。相反,KEM利用公鑰的屬性派生一個相關的對稱金鑰,這不需要填充。

此次新增了javax.crypto.KEM、javax.crypto.KEMSpi
package javax.crypto;

public class DecapsulateException extends GeneralSecurityException;

public final class KEM {

    public static KEM getInstance(String alg)
        throws NoSuchAlgorithmException;
    public static KEM getInstance(String alg, Provider p)
        throws NoSuchAlgorithmException;
    public static KEM getInstance(String alg, String p)
        throws NoSuchAlgorithmException, NoSuchProviderException;

    public static final class Encapsulated {
        public Encapsulated(SecretKey key, byte[] encapsulation, byte[] params);
        public SecretKey key();
        public byte[] encapsulation();
        public byte[] params();
    }

    public static final class Encapsulator {
        String providerName();
        int secretSize();           // Size of the shared secret
        int encapsulationSize();    // Size of the key encapsulation message
        Encapsulated encapsulate();
        Encapsulated encapsulate(int from, int to, String algorithm);
    }

    public Encapsulator newEncapsulator(PublicKey pk)
            throws InvalidKeyException;
    public Encapsulator newEncapsulator(PublicKey pk, SecureRandom sr)
            throws InvalidKeyException;
    public Encapsulator newEncapsulator(PublicKey pk, AlgorithmParameterSpec spec,
                                        SecureRandom sr)
            throws InvalidAlgorithmParameterException, InvalidKeyException;

    public static final class Decapsulator {
        String providerName();
        int secretSize();           // Size of the shared secret
        int encapsulationSize();    // Size of the key encapsulation message
        SecretKey decapsulate(byte[] encapsulation) throws DecapsulateException;
        SecretKey decapsulate(byte[] encapsulation, int from, int to,
                              String algorithm)
                throws DecapsulateException;
    }

    public Decapsulator newDecapsulator(PrivateKey sk)
            throws InvalidKeyException;
    public Decapsulator newDecapsulator(PrivateKey sk, AlgorithmParameterSpec spec)
            throws InvalidAlgorithmParameterException, InvalidKeyException;

}
它主要是提供了newEncapsulator、newDecapsulator方法,使用示例如下
// Receiver side
KeyPairGenerator g = KeyPairGenerator.getInstance("ABC");
KeyPair kp = g.generateKeyPair();
publishKey(kp.getPublic());

// Sender side
KEM kemS = KEM.getInstance("ABC-KEM");
PublicKey pkR = retrieveKey();
ABCKEMParameterSpec specS = new ABCKEMParameterSpec(...);
KEM.Encapsulator e = kemS.newEncapsulator(pkR, specS, null);
KEM.Encapsulated enc = e.encapsulate();
SecretKey secS = enc.key();
sendBytes(enc.encapsulation());
sendBytes(enc.params());

// Receiver side
byte[] em = receiveBytes();
byte[] params = receiveBytes();
KEM kemR = KEM.getInstance("ABC-KEM");
AlgorithmParameters algParams = AlgorithmParameters.getInstance("ABC-KEM");
algParams.init(params);
ABCKEMParameterSpec specR = algParams.getParameterSpec(ABCKEMParameterSpec.class);
KEM.Decapsulator d = kemR.newDecapsulator(kp.getPrivate(), specR);
SecretKey secR = d.decapsulate(em);

// secS and secR will be identical

JEP 453: Structured Concurrency (Preview)

在JDK19的JEP 428: Structured Concurrency (Incubator)作為第一次incubator
在JDK20的JEP 437: Structured Concurrency (Second Incubator)作為第二次incubator
此次在JDK21則作為preview,使用示例如下
Response handle() throws ExecutionException, InterruptedException {
    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
        Supplier<String>  user  = scope.fork(() -> findUser());
        Supplier<Integer> order = scope.fork(() -> fetchOrder());

        scope.join()            // Join both subtasks
             .throwIfFailed();  // ... and propagate errors

        // Here, both subtasks have succeeded, so compose their results
        return new Response(user.get(), order.get());
    }
}

細項解讀

上面列出的是大方面的特性,除此之外還有一些api的更新及廢棄,主要見JDK 21 Release Notes,這裡舉幾個例子。

新增項

  • Math.clamp() and StrictMath.clamp() Methods (JDK-8301226)
  • New String indexOf(int,int,int) and indexOf(String,int,int) Methods to Support a Range of Indices (JDK-8302590)
  • New splitWithDelimiters() Methods Added to String and java.util.regex.Pattern (JDK-8305486)
  • System.exit() and Runtime.exit() Logging (JDK-8301627)
  • The java.net.http.HttpClient Is Now AutoCloseable (JDK-8267140)
  • New StringBuilder and StringBuffer repeat Methods (JDK-8302323)
  • Last Resort G1 Full GC Moves Humongous Objects (JDK-8191565)

移除項

  • Removed SECOM Trust System's RootCA1 Root Certificate (JDK-8295894)
  • java.io.File's Canonical Path Cache Is Removed (JDK-8300977)
  • Removal of the java.compiler System Property (JDK-8041676)
  • The java.lang.Compiler Class Has Been Removed (JDK-8205129)
  • Remove the JAR Index Feature (JDK-8302819)
  • Removal of G1 Hot Card Cache (JDK-8225409)
  • Obsolete Legacy HotSpot Parallel Class Loading Workaround Option -XX:+EnableWaitForParallelLoad Is Removed (JDK-8298469)
  • The MetaspaceReclaimPolicy Flag has Been Obsoleted (JDK-8302385)

廢棄項

  • Deprecate GTK2 for Removal (JDK-8280031)
  • Deprecate JMX Subject Delegation and the JMXConnector.getMBeanServerConnection(Subject) Method for Removal (JDK-8298966)

重要bug修復

  • Error Computing the Amount of Milli- and Microseconds between java.time.Instants (JDK-8307466)
  • Disallow Extra Semicolons Between "import" Statements (JDK-8027682)

已知問題

  • JVM May Crash or Malfunction When Using ZGC and Non-Default ObjectAlignmentInBytes (JDK-8312749)
  • Validations on ZIP64 Extra Fields (JDK-8313765)
  • java.util.regex.MatchResult Might Throw StringIndexOutOfBoundsException on Regex Patterns Containing Lookaheads and Lookbehinds (JDK-8132995)
  • JVM May Hang When Using Generational ZGC if a VM Handshake Stalls on Memory (JDK-8311981)

其他事項

  • ObjectInputStream::readObject() Should Handle Negative Array Sizes without Throwing NegativeArraySizeExceptions (JDK-8306461)
  • File::listRoots Changed to Return All Available Drives on Windows (JDK-8208077)
  • Thread.sleep(millis, nanos) Is Now Able to Perform Sub-Millisecond Sleeps (JDK-8305092)
  • FileChannel.transferFrom Extends File if Called to Transfer Bytes to the File (JDK-8303260)
  • Clarification of the Default Charset Initialization with file.encoding (JDK-8300916)
  • java.util.Formatter May Return Slightly Different Results on double and float (JDK-8300869)
  • JVM TI ThreadStart and ThreadEnd Events Not Sent for Virtual Threads (JDK-8307399)
  • Add final Keyword to Some Static Methods (JDK-8302696)

小結

Java21主要有如下幾個特性

doc

相關文章