Unlock Java 21: Virtual Threads, Generational ZGC, and ARMS Monitoring Guide
This article introduces Java 21’s major enhancements—including lightweight virtual threads and generational ZGC—provides code samples for creating virtual threads and enabling ZGC, demonstrates a Spring Boot 3.x application using record patterns, and explains how to monitor Java 21 services with Alibaba Cloud ARMS on ACK.
Java 21 Overview
On September 19, Java 21 became the latest LTS release, bringing 15 new features such as virtual threads, generational ZGC, and various language and runtime improvements that boost performance, stability, and developer productivity.
Key New Features
1. Virtual Threads
Virtual threads are lightweight user‑mode threads managed by the JDK runtime, allowing an M:N mapping to OS kernels. They dramatically reduce the overhead of creating, switching, and destroying threads in high‑concurrency scenarios.
Thread vt = Thread.startVirtualThread(() -> {}); Thread.ofVirtual().unstarted(() -> {}); vt.start(); ThreadFactory tf = Thread.ofVirtual().factory(); Thread vt = tf.newThread(() -> {}); vt.start(); ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); ThreadFactory tf = Thread.ofVirtual().factory(); Thread vt = tf.newThread(() -> {}); executor.submit(vt);2. Generational ZGC
Java 21 adds generational support to ZGC, enabling more frequent collection of young‑generation objects and reducing pause times. To enable it, start the JVM with -XX:+UseZGC -XX:+ZGenerational.
$ java -XX:+UseZGC -XX:+ZGenerational ...3. Other Notable Additions
Underscore‑named variables for unused values, similar to Go.
Anonymous class and instance main methods, allowing void main() { … } syntax.
Building a Java 21 Spring Boot Application
After installing JDK 21, create a Spring Boot 3.x project that uses the new Record Patterns feature:
<?xml version="1.0" encoding="UTF-8"?></code><code><project xmlns="http://maven.apache.org/POM/4.0.0" ...></code><code> <properties><java.version>21</java.version></properties></code><code> <dependencies>…<dependency>org.springframework.boot:spring-boot-starter-web</dependency>…</dependencies></code><code><build>…<plugin>org.springframework.boot:spring-boot-maven-plugin</plugin>…</build> @SpringBootApplication</code><code>public class DemoApplication {</code><code> public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }</code><code> record Point(int x, int y) {}</code><code> @RestController static class DemoController {</code><code> @RequestMapping("/demo") String demo(@RequestParam String type) {</code><code> Object obj = "record".equals(type) ? new Point(1,2) : "object";</code><code> return solve(obj);</code><code> }</code><code> private String solve(Object o) {</code><code> if (o instanceof Point(int x, int y)) return "Point: " + x + ":" + y;</code><code> return "Invalid Point";</code><code> }</code><code> }</code><code>}Monitoring Java 21 with Alibaba Cloud ARMS
ARMS 3.1.0 probe adds native support for Java 21. To monitor Java 21 applications on Alibaba Cloud Container Service for Kubernetes (ACK), install the ack-onepilot component, then add two pod labels ( armsPilotCreateAppName and armsPilotAutoEnable=on) to the deployment.
After deployment, ARMS automatically instruments the JVM, and the console displays metrics such as application metadata, request traces, and CPU/memory diagnostics. Sample logs show the probe successfully attached.
ARMS Probe Enhancements in 3.X
Improved coverage for async frameworks (Reactor Netty, Vert.x) and databases (OceanBase, PostgreSQL, Kafka).
Reduced CPU overhead by up to 65 % for async workloads and shortened probe startup time to under 5 seconds.
Code‑hotspot feature integrates Async Profiler to generate on‑CPU and off‑CPU flame graphs linked to trace IDs.
References
OpenJDK Java 21 project: https://openjdk.org/projects/jdk/21/
Java 21 JEPs: https://openjdk.org/jeps/439, https://openjdk.org/jeps/440
ARMS documentation and probe download page (version 3.1.0).
Async Profiler: https://github.com/async-profiler/async-profiler
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
