Welcome to the future of Java. With this book, you'll explore the transformative world of Java 21's key feature: virtual threads. Remember struggling with the cost of thread creation, encountering limitations on scalability, and facing difficulties in achieving high throughput? Those days are over. This practical guide takes you from Java 1.0 to the cutting-edge advancements of Project Loom.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# Modern Concurrency in Java
## 【One-Line Pitch】
A practical guide to Java's evolution from traditional platform threads to the revolutionary virtual threads of JDK 21, showing developers how to build highly scalable, high-throughput concurrent applications without the historical costs of thread management. Essential reading for Java developers who want to modernize their concurrency approach and understand Project Loom's impact.
## 【Book Arc】
- **Opening (~0%–10%)**: Establishes the fundamental problem—traditional Java threads are heavyweight, expensive to create, and costly to context-switch. Uses a concrete order-processing example to demonstrate exception propagation through call stacks and measures thread creation limits in real environments.
- **Early (~10%–23%)**: Introduces the two kinds of threads now in Java—platform threads (OS-managed, one-to-one kernel mapping) versus virtual threads (JVM-managed, lightweight, millions possible). Covers basic virtual thread creation patterns, the Thread Builder API, and transitioning existing executor code. Demonstrates aggregating multiple asynchronous API calls with virtual threads and introduces semaphores for rate limiting and resource control.
- **Early (~23%–32%)**: Explores virtual thread internals—ThreadLocal challenges and pinning issues with synchronized blocks, the Foreign Function & Memory API as JNI's replacement, and scoped values as a safer alternative. Includes practical JFR (Java Flight Recorder) configuration for detecting VirtualThreadPinned events and thread dump analysis showing virtual thread containers.
- **Middle (~39%–48%)**: Dives into the executor framework, Callable and Future for handling task results, and ForkJoinPool's work-stealing design. Explains CAS operations for non-blocking synchronization and why ForkJoinPool's architecture makes it the natural implementation foundation for virtual threads.
- **Middle (~48%–end)**: Explains the underlying mechanics—how continuations enable virtual thread suspension and resumption, the JVM-wide I/O poller that unparks threads when data arrives, and platform-specific polling mechanisms (kqueue on macOS, epoll on Linux, wepoll on Windows).
## 【Key Takeaways】
- **Platform threads are the original bottleneck** (Opening): Traditional Java threads maintain one-to-one mapping with OS kernel threads, making creation expensive in both memory and CPU—context switching overhead becomes significant under high load. This motivates everything that follows.
- **Virtual threads are JVM-managed and nearly free** (Early): Unlike platform threads that rely on OS scheduling, virtual threads are managed entirely by the JVM, allowing millions to be created without exhausting system resources—the core scalability breakthrough of JDK 21.
- **Blocking becomes cheap with virtual threads** (Early): The burden of blocking while waiting for asynchronous tasks is lifted—developers can call blocking `get()` on Futures without performance penalties, dramatically simplifying code that aggregates multiple async operations.
- **Semaphores remain essential for resource control** (Early): Even with virtual threads, semaphores provide critical gatekeeping for shared resources—limiting concurrent database connections to match licenses or rate-limiting external API requests to 100 per minute.
- **Synchronized blocks cause pinning; ReentrantLock doesn't** (Early): Virtual threads sleeping inside synchronized blocks remain pinned to carrier threads (captured as VirtualThreadPinned events in JFR), while ReentrantLock allows unmounting—making lock choice a performance decision.
- **ThreadLocal has memory implications with virtual threads** (Early): The overhead challenges of ThreadLocal in virtual thread environments motivate alternatives like scoped values, which offer immutability and bounded lifetimes better suited for passing data between threads safely.
- **ForkJoinPool's CAS-based work-stealing design powers virtual threads** (Middle): ForkJoinPool uses CAS operations for work-stealing queues to keep threads productive rather than waiting—the same non-blocking efficiency that makes it the natural implementation foundation for virtual threads.
- **Continuations and I/O polling make virtual threads work** (Middle): When virtual threads perform I/O, continuations pause and remove them from carrier threads; a JVM-wide poller (using kqueue, epoll, or wepoll) monitors operations and unparks threads when data arrives.
## 【Reading Tips】
- **Skim the opening thread-cost measurements** (~0%–10%) if you already understand why traditional threads are expensive—the key insight is the one-to-one kernel mapping and context-switch overhead, not the specific benchmark numbers.
- **Deep-read the virtual thread creation patterns** (~13%–19%): The Thread Builder API, `Thread.startVirtualThread()`, and `Executors.newVirtualThreadPerTaskExecutor()` are the practical foundations you'll use daily—master these before moving on.
- **Pay special attention to the pinning discussion** (~29%): Understanding when virtual threads get pinned to carrier threads (synchronized blocks) versus when they don't (ReentrantLock) is crucial for avoiding subtle performance traps in production.
- **The ForkJoinPool and continuation sections** (~42%–48%) are the most technically dense—skim if you're primarily interested in application-level concurrency, but deep-read if you want to understand why virtual threads work the way they do.
- **Study the semaphore examples carefully** (~19%–23%): The rate-limiting pattern with virtual threads demonstrates the practical philosophy—intentional bottlenecks are fine when blocking is cheap.
## 【Coverage Limits】
Excerpts do not cover structured concurrency APIs, specific performance benchmarks comparing virtual threads against reactive frameworks, or detailed migration strategies for existing reactive codebases. The guide focuses on the conceptual foundation and practical patterns for virtual threads rather than exhaustive API reference.
##
Page 17
itionally, I am grateful to my father-in-law and mother-in- law for their enthusiasm and encouragement regarding my work. Their warm support and genuine exci...
String> future = virtualExecutor.submit(this::callService); // Process the future result } This is especially helpful in large projects where immediate refac...
divide-and-conquer algorithm. The basic idea is that we can divide a big task into smaller tasks until they are simple enough to compute independently. Howev...
rently. The main thread waits at join() until the last task completes, after which join() returns the Stream of results, which we then print. In the failure...
s are doing which work in thread dumps and profiling tools. Consider the following code snippets: Let’s examine how this joiner handles different subtask out...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Modern Concurrency in Java (A N M Bazlur Rahman)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Modern Concurrency in Java (A N M Bazlur Rahman)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment