跪拜 Guibai
← All articles
Backend

Java's Four-Generation Leap: From Lambdas to Virtual Threads

By 神奇小汤圆 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams stuck on JDK 8 are now three LTS versions behind, missing compiler-enforced null-safety through pattern matching, sealed class hierarchies that enable exhaustive switch expressions, and a threading model that removes the need for reactive frameworks in high-concurrency services.

Summary

Java's release cadence shifted from multi-year feature dumps to a strict 6-month train model, with LTS versions now arriving every two years. JDK 8 introduced Lambda, Stream, Optional, and the java.time API, fundamentally changing how Java code is written. JDK 17 finalized preview features like Records, Sealed Classes, and pattern matching, forming a cohesive modern syntax.

JDK 21 delivered Virtual Threads, which move thread management from the OS kernel into the JVM. Unlike platform threads that consume ~1MB of stack and involve kernel-switching costs, virtual threads use continuations to unmount during blocking calls and resume on any available carrier thread, with stack memory growing on demand from a few hundred bytes. This makes blocking I/O cheap enough that one-thread-per-request architectures can scale to millions of concurrent connections without reactive workarounds.

The non-LTS releases between these milestones incubated the features that later stabilized: JDK 9's module system, JDK 10's `var`, JDK 11's standardized HttpClient, and JDK 15's text blocks all became production-ready in subsequent LTS versions.

Takeaways
— JDK 8's Lambdas are not syntactic sugar for anonymous inner classes; the compiler uses `invokedynamic` and `LambdaMetafactory` to generate implementations at runtime, avoiding a proliferation of `.class` files.
— Parallel streams share the global `ForkJoinPool.commonPool()`, so a blocking operation in one request can stall parallel stream execution across an entire web application.
— JDK 17 finalized Records, Sealed Classes, and pattern matching for `instanceof`, creating a feature set that allows the compiler to verify exhaustiveness in type checks.
— Virtual Threads in JDK 21 unmount from their carrier thread when blocked on I/O, sleep, or locks, then resume on any available carrier thread, making blocking operations cheap enough for millions of concurrent tasks.
— Oracle JDK 17 and later are free for commercial use under the NFTC license, but many organizations have standardized on Adoptium, Corretto, or Azul distributions.
— JDK 18 changed the default charset to UTF-8, eliminating a long-standing cross-platform encoding trap.
Conclusions

Parallel streams are a footgun in server-side Java because the shared fork-join pool creates contention between unrelated requests, a design choice that contradicts the isolation most web frameworks assume.

The shift from a 3-year to a 2-year LTS cadence pressures organizations to adopt a continuous upgrade discipline rather than treating Java versions as decade-long platforms.

Virtual Threads do not just improve throughput; they eliminate the architectural justification for reactive programming in many cases, which could shrink the addressable market for frameworks like WebFlux and RxJava.

Concepts & terms
Virtual Threads
JVM-managed threads introduced in JDK 21 that do not map 1:1 to OS threads. They use continuations to save state, unmount from a carrier thread during blocking operations, and resume on any available carrier thread. Stack memory grows on demand from a few hundred bytes, enabling millions of concurrent threads.
Sealed Classes
A JDK 17 feature that restricts which classes can extend or implement a given class or interface. When combined with pattern matching in a switch expression, the compiler can verify that all permitted subtypes are covered, providing exhaustiveness checking.
invokedynamic
A JVM instruction used by Lambda expressions to defer the creation of the functional interface implementation to runtime via `LambdaMetafactory`, rather than generating an anonymous inner class at compile time.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗