---
title: 祝、JDK21  リリース
tags: 
author: [紅月さん@がんばらない](https://www.docswell.com/user/koduki)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/8JDDRL85JG.jpg?width=480
description: Java21のリリースを記念した以下の配信の資料でうｓ。 https://youtu.be/QPJpUwXA3f4
published: October 04, 23
canonical: https://www.docswell.com/s/koduki/KNRXYD-2023-10-04-024748
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/8JDDRL85JG.jpg)

祝！
Java 21
リリース


# Page. 2

![Page Image](https://bcdn.docswell.com/page/VEPXW28DJ8.jpg)

Agenda
•Open JDK 21サマリ
•Java21の新機能


# Page. 3

![Page Image](https://bcdn.docswell.com/page/27VL8DNGEQ.jpg)

Every 6-month Release and LTS
The arrival of java 21! – Inside.java


# Page. 4

![Page Image](https://bcdn.docswell.com/page/5JGX5Y5D7L.jpg)

LTS (long-term support)
• 6か月毎に出るJDKはどれもProduction Qualityだが、長期サポートが保
証されているのはLTS版となる
• こちらはJava 21より従来の３年に一度から２年に一度に変更 ※1
• Oracleは商用サポートをExtended Supportとして2031年までの8年間提
供する
• Redhat含めた他社のJDKやOSSのコミュニティサポートも概ね類似し
た期間になるが、基本的には関連製品（例えばRedhatならRHEL、
AzulならPayara）と合わせてもあるので、個別に要チェック
※1 実際は各ベンダー次第だが大手のOracleやRedhatに原則合わせている


# Page. 5

![Page Image](https://bcdn.docswell.com/page/47Q8ZMZXEP.jpg)

The arrival of java 21! – Inside.java
Issues fixed in JDK 15 per organization


# Page. 6

![Page Image](https://bcdn.docswell.com/page/KE423V3271.jpg)

余談: OpenJDKとディストリビューション
• 現在のJavaはOracle１社ではなく、OSSとして多くの企業で開発
• それらをLinuxのように各ベンダーがビルドしてディストリ
ビューションとして公開(Oracle OpenJDK, Redhat Open JDK, Eclipse
Temurin, Amazon Correct など)
• OpenJDK8のころと違い、Oracle版とOpenJDKで差があるといった
こともないので、Oracleが公式バイナリというわけではない
• 各社毎にサポートが異なっており、Oracleのように商用サポート
の展開もあれば、コミュニティサポートのみを展開するケース
もある
• 詳しくは以下を参考
OpenJDKと各種JDKディストリビューションの情報源まとめ (zenn.dev)


# Page. 7

![Page Image](https://bcdn.docswell.com/page/L71M131KJG.jpg)



# Page. 8

![Page Image](https://bcdn.docswell.com/page/G7W38P8P72.jpg)

Adoptium Temurinのサポート予定
First Availability Latest Release
Next Release
Due
End of
Availability [1]
Java 21 (LTS)
Expected
Sep 2023
Not available
Expected
Sep 2023
Expected Sep
2029
Java 17 (LTS)
Sep 2021
18 Jul 2023
jdk-17.0.8+7
17 Oct 2023
jdk-17.0.9
At least Oct 2027
Java 11 (LTS)
Sep 2018
18 Jul 2023
jdk-11.0.20+8
17 Oct 2023
jdk-11.0.21
At least Oct 2024
Java 8 (LTS)
Mar 2014
21 Jul 2023
jdk8u382-b05
17 Oct 2023
jdk8u392
At least Nov 2026


# Page. 9

![Page Image](https://bcdn.docswell.com/page/4JZV8K8673.jpg)

オラクル、「Java 21」をリリース--最新の長期サポートバージョンに - ZDNET Japan


# Page. 10

![Page Image](https://bcdn.docswell.com/page/YE65PQPLJV.jpg)

JDK18 – 21までの機能追加一覧
JEP名
JEP番号
JDK 18
JDK 19
JDK 20
JDK 21
UTF-8 by Default
400
GA
-
-
-
Simple Web Server
408
GA
-
-
-
Code Snippets in Java API
Documentation
413
GA
-
-
-
Reimplement Core Reflection with
416
Method Handles
GA
-
-
-
Internet-Address Resolution SPI
GA
-
-
-
418


# Page. 11

![Page Image](https://bcdn.docswell.com/page/GE5YKRKME4.jpg)

JEP名
Deprecate Finalization for
Removal
JEP番号
JDK 18
JDK 19
JDK 20
JDK 21
421
GA
-
-
-
Record Patterns
405/432
-
Preview
Preview
GA
Linux/RISC-V Port
422
-
GA
-
-
Vector API
417/426/438
Incubator
Incubator
Incubator
Incubator
Foreign Function &amp; Memory
API
419/424/434/442 Incubator
Preview
Preview
Preview
Pattern Matching for switch
420/427/433/441 Preview
Preview
Preview
GA
Scoped Values
429/446
Incubator
-
-
Preview
Structured Concurrency
428/437
Incubator
-
Incubator
Preview
Virtual Threads
425/436/444
-
Preview
Preview
GA
String Templates
430
-
-
-
Preview


# Page. 12

![Page Image](https://bcdn.docswell.com/page/972GWDWR7R.jpg)

JEP名
Sequenced Collections
JEP番号
431
JDK 18
-
JDK 19
-
JDK 20
-
JDK 21
GA
Generational ZGC
439
-
-
-
GA
Unnamed Patterns and
Variables
443
-
-
-
Preview
Unnamed Classes and Instance
445
Main Methods
-
-
-
Preview
Deprecate the Windows 32-bit
449
x86 Port for Removal
-
-
-
GA
Prepare to Disallow the
Dynamic Loading of Agents
451
-
-
-
GA
Key Encapsulation Mechanism
452
API
-
-
-
GA


# Page. 13

![Page Image](https://bcdn.docswell.com/page/DJYVL2L5JM.jpg)

特に注目したいGA機能
• UTF-8 by Default
• Simple Web Server
• Virtual Threads
• Pattern Matching for switch / Record Patterns


# Page. 14

![Page Image](https://bcdn.docswell.com/page/V7NM4844E8.jpg)

JEP 400: UTF-8 by Default
• 従来のJavaではファイル読み込み時等に文字セット
を指定しなければOS標準となっていた
• MacならUTF-8、WindowsならMS932など
• 多くの場合、明示的に指定していると思うが、古い
Windows向けのアプリなどはMS932を前提にしてい
る事も多く注意が必要
• 従来と同じ動作にするには-J-Dfile.encoding=COMPAT
を起動時に指定する
• ソースコードがShift_JISの場合も影響ありなので注意


# Page. 15

![Page Image](https://bcdn.docswell.com/page/YJ9NQKQ4J3.jpg)

JEP 408: Simple Web Server
• ツールとしてシンプルなWebサーバが付属。Python
やRubyでよくやる奴がついにJavaに。
• Windowsだとコンソールは文字化けするが、実用に
は問題なし


# Page. 16

![Page Image](https://bcdn.docswell.com/page/GJ8KGRGVED.jpg)

JEP 408: Simple Web Server
オプション
用途
-h / -? / --help
ヘルプメッセージの表示
-b addr / --bind-addess addr
アドレスをバインドする。デフォルトはループバック
-d dir / --directory dir
提供するディレクトリ。デフォルトはカレントディレクトリ
-o level or --output level
出力フォーマット。 none / info / verbose デフォルトはinfo
-p port or --port port
ポート番号。デフォルトは8000
-version or --version
バージョンの出力
Ref: Java 18新機能まとめ - Qiita


# Page. 17

![Page Image](https://bcdn.docswell.com/page/LJLZGKGR7R.jpg)

JEP 408: Simple Web Server
PS C:¥Users¥koduki&gt; jshell
| JShellへようこそ -- バージョン21
| 概要については、次を入力してください: /help intro
jshell&gt; import com.sun.net.httpserver.SimpleFileServer
jshell&gt; import com.sun.net.httpserver.SimpleFileServer.OutputLevel
jshell&gt; var server = SimpleFileServer.createFileServer(new InetSocketAddress(8000),
Path.of(&quot;c:/Users/koduki&quot;), OutputLevel.VERBOSE)
server ==&gt; sun.net.httpserver.HttpServerImpl@2a40cd94
jshell&gt; server.start()
Ref: Java 18新機能まとめ - Qiita


# Page. 18

![Page Image](https://bcdn.docswell.com/page/47MVQ2QP7W.jpg)

JEP 444: Virtual Threads
• 従来のJavaのスレッドはOSネイティブのPlatform Threads
• これはエンコードのようなCPUをヘビーに使う処理には向い
ているが、WebサーバのようにI/O待ちが多い処理では、
CPUの使い方が非効率
• GoroutineのようなPlatform Threadsと1:1にならないM:Nの並
行機構を作る事で、従来より大量のスレッドを省リソース
で扱いCPUを効果的に使う
• Reactive Programingでも似たような事が出来たがVirtual
Threadsはより直感的に扱うことが期待できる
• 従来のスレッドに比べてhigher throughputではあるがlower
latency ではないのには注意
• Thread Poolも原則作成するのは非推奨


# Page. 19

![Page Image](https://bcdn.docswell.com/page/P7R28M8YJ9.jpg)

通常のスレッド
Java19 Official GA! See how virtual threads can dramatically increase system throughput - SoByte


# Page. 20

![Page Image](https://bcdn.docswell.com/page/PJXD8V84EX.jpg)

Virtual Threads
Java19 Official GA! See how virtual threads can dramatically increase system throughput - SoByte


# Page. 21

![Page Image](https://bcdn.docswell.com/page/3JKXKQKPED.jpg)

Blocking Code v.s. Reactive Code
Blocking Code
private void one(ServerRequest req, ServerResponse res) {
String response = callRemote(client());
res.send(response);
}
Reactive Code
private void one(ServerRequest req, ServerResponse res) {
Single&lt;String&gt; response = client.get()
.request(String.class);
response.forSingle(res::send)
.exceptionally(res::send);
}
JEP 425: Java Virtual Threads to Deliver Improved Throughput (infoq.com)


# Page. 22

![Page Image](https://bcdn.docswell.com/page/LE35ZPZ4J5.jpg)

Virtual Threadsの初期化の仕方
// 従来のPlatform Threadsの初期化
Thread t = new Thread(() -&gt; System.out.println(&quot;hello&quot;));
// JDK 21から可能なPlatform Threadsの初期化
Thread t = Thread.ofPlatform().unstarted(() -&gt; System.out.println(&quot;hello&quot;));
// Virtual Threadsの初期化
Thread t = Thread.ofVirtual().unstarted(() -&gt; System.out.println(&quot;hello&quot;));
// ExecutorServiceでVirtual Threadsを使う場合
ExecutorService ex = Executors.newVirtualThreadPerTaskExecutor();


# Page. 23

![Page Image](https://bcdn.docswell.com/page/8EDDRLR5EG.jpg)

Platform Threads – 100


# Page. 24

![Page Image](https://bcdn.docswell.com/page/V7PXW2WDE8.jpg)

Virtual Threads – 100


# Page. 25

![Page Image](https://bcdn.docswell.com/page/2JVL8D8G7Q.jpg)

Virtual Threads – 10万スレッド


# Page. 26

![Page Image](https://bcdn.docswell.com/page/5EGX5YZDJL.jpg)

Platform Threads – 10万スレッド


# Page. 27

![Page Image](https://bcdn.docswell.com/page/4JQ8ZMLX7P.jpg)



# Page. 28

![Page Image](https://bcdn.docswell.com/page/K7423VD2J1.jpg)

JEP 441: Pattern Matching for switch
JEP 440: Record Patterns
• Switch式が大幅に強化
• Nullに対応したので条件式が簡単に書ける
• 型のパターンマッチでオーバロードっぽいことも
• sealed classやRecord型と組み合わせたより柔軟な処理
が可能に


# Page. 29

![Page Image](https://bcdn.docswell.com/page/LJ1M13ZKEG.jpg)

パターンマッチの強化
String f(Object o) {
return switch (o) {
case String s when s.isBlank() -&gt; &quot;0000&quot;;
case String s -&gt; s;
case Integer i -&gt; &quot;%04d&quot;.formatted(i);
case null, default -&gt; &quot;0000&quot;;
};
}
jshell&gt; f(1)
$11 ==&gt; &quot;0001&quot;
jshell&gt; f(2)
$12 ==&gt; &quot;0002&quot;
jshell&gt; f(null)
$13 ==&gt; &quot;0000&quot;
jshell&gt; f(&quot;&quot;)
$14 ==&gt; &quot;0000&quot;
jshell&gt; f(&quot;1234&quot;)
$15 ==&gt; &quot;1234&quot;


# Page. 30

![Page Image](https://bcdn.docswell.com/page/GJW38P9PJ2.jpg)

パターンマッチとレコード型
sealed
record
record
record
record
record
interface Expr permits Add, Sub, Mul,
Add(Expr left, Expr right) implements
Sub(Expr left, Expr right) implements
Mul(Expr left, Expr right) implements
Div(Expr left, Expr right) implements
Num(int n) implements Expr {}
Div,
Expr
Expr
Expr
Expr
Num {}
{}
{}
{}
{}


# Page. 31

![Page Image](https://bcdn.docswell.com/page/4EZV8K96J3.jpg)

パターンマッチとレコード型
public int eval(Expr expr) {
return switch (expr) {
case Add(Expr l, Expr
case Sub(Expr l, Expr
case Mul(Expr l, Expr
case Div(Expr l, Expr
case Num(int n) -&gt; n;
};
}
r)
r)
r)
r)
-&gt;
-&gt;
-&gt;
-&gt;
eval(l)
eval(l)
eval(l)
eval(l)
+
*
/
eval(r);
eval(r);
eval(r);
eval(r);
jshell&gt; eval(new Add(new Num(1), new Num(2)));
$8 ==&gt; 3
jshell&gt; eval(new Mul(new Add(new Num(1), new Num(2)), new Num(3)));
$9 ==&gt; 9


# Page. 32

![Page Image](https://bcdn.docswell.com/page/Y765PQKLEV.jpg)

特に注目したいPreview / Incubation機能
• JEP 445: Unnamed Classes and Instance Main
Methods
• JEP 430: String Templates


# Page. 33

![Page Image](https://bcdn.docswell.com/page/G75YKRPM74.jpg)

まとめ
• Java17より2年ぶりのLTS。8年間前後のサポート期間
• Temurin基準だとJava 11が24年、17が27年、21が29年サポート
• 世代別ZGCやVirtual Threadsを始めとした、性能の大幅な改善
につながる新機能や、Switch式/パターンマッチの大幅な強化
が目玉
• よりモダンな言語に。
• 一方で、UTF-8のデフォルト化などの非互換性には要注意


