Video in progress
When you move a service to virtual threads, what do you check at the JVM level: pinning, memory and ThreadLocal?
Virtual threads help in code with a lot of blocking I/O, but pinning can tie up carrier threads, heavy objects in a ThreadLocal can get copied a million times, and the resources underneath (the DB pool) may not survive unbounded parallelism. I check these with JFR and a load test.
📖 Answer
In Spring Boot 3.2+, spring.threads.virtual.enabled=true is a single line. But here is what changes at the JVM level:
Pinning. When a virtual thread blocks, the carrier (a platform thread in the ForkJoinPool) must be freed. In JDK 21-23, blocking inside a synchronized block or a native frame "nails" the virtual thread to its carrier. The number of carriers equals the number of cores, so a few pinned threads can stall the whole application. In JDK 24 (JEP 491), synchronized and Object.wait() no longer pin, while blocking inside a native method or a foreign function still does. To detect it:
# JFR event: jdk.VirtualThreadPinned (pinning longer than the default 20 ms)
jfr print --events jdk.VirtualThreadPinned rec.jfr
In JDK 21 there was also -Djdk.tracePinnedThreads=full (removed later). synchronized inside old JDBC drivers and libraries is the most common cause.
Memory. A virtual thread's stack is kept in the heap and copied when it blocks. 100 thousand parallel requests means 100 thousand stack chunks in the heap, which is a new load on the GC. With framework code that has deep stacks, this is noticeable. Recheck the heap budget in a load test.
ThreadLocal. Every virtual thread gets its own copy. A SimpleDateFormat or buffer that used to be cached across 200 platform threads is now created anew on every request and kept a million times. Such "per-thread caches" have to be removed or replaced with a shared thread-safe object.
Resources underneath. Before, Tomcat's 200 threads were a natural limit. Now 10,000 requests wait at once for the DB pool's 20 connections, and timeouts increase. You need to limit parallelism explicitly with a semaphore or bulkhead.
Diagnostics. Virtual threads don't show up in a thread dump: use jcmd <pid> Thread.dump_to_file -format=json out.json.
For CPU-bound work, virtual threads give nothing, and an ordinary pool stays the right choice there.
Sign up to mark studied questions and save your progress.