Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

On a Java 21 service, enabling virtual threads made throughput collapse under load. A thread dump shows all carrier threads busy, many virtual threads waiting, and frames inside a JDBC driver's synchronized method doing socket reads. Explain and give your options.

On Java 21, the driver's synchronized method pins each virtual thread to its carrier, so its socket read blocks the carrier's OS thread instead of unmounting. There are only as many carriers as CPU cores, so a few slow queries can occupy every carrier, and all other virtual threads wait even though the CPU is idle. The options are to upgrade to JDK 24 or later, where synchronized no longer pins in this case, or to upgrade the driver to a version that uses ReentrantLock instead of synchronized, with short-term mitigations in between; pinning is detected with the JFR jdk.VirtualThreadPinned event, since jdk.tracePinnedThreads was removed in JDK 24.

Why this question is asked

This is the incident that made many teams cautious about virtual threads on Java 21: the switch is flipped, load tests look fine at low traffic, and under real load throughput drops to a fraction of what platform threads achieved. The thread dump tells the whole story once we know how to read it. Interviewers ask this debugging question to see whether we can connect the evidence to the mechanism and then choose a fix with its costs in mind. In this answer, we will read the thread dump, explain why the carriers are stuck, work out why a few slow queries are enough, confirm it with JFR, and then go through the options from the best long-term fix to the stopgaps.

1Reading the Evidence

The thread dump shows three things. All carrier threads, the ForkJoinPool workers of the virtual thread scheduler, are busy. Many virtual threads are waiting, but they are not blocked on anything in particular; they are runnable and simply have no carrier to run on. And the frames on the busy carriers are inside a JDBC driver's synchronized method, doing socket reads.

Java
"ForkJoinPool-1-worker-3" carrier, running VirtualThread[#5123]
at sun.nio.ch.NioSocketImpl.read(...) <- blocking read, carrier blocked too
at com.acme.jdbc.PgStream.receive(...)
at com.acme.jdbc.QueryExecutor.execute(...) - locked <0x7a1> (a QueryExecutor) <- synchronized
at com.acme.jdbc.Statement.executeQuery(...)
at com.example.orders.OrderRepository.find(...)
(the same shape on worker-1 to worker-8; 4,200 virtual threads runnable, waiting for a carrier)

The locked line is the key. The virtual thread entered a synchronized method of the driver, then reached a socket read. On Java 21, that combination pins: the virtual thread cannot unmount while it holds a monitor, so the read blocks the carrier's OS thread for as long as the database takes to answer. (The driver name and frames here are illustrative; the shape is what to look for.)

2Why a Few Slow Queries Stall Everything

The scheduler has about one carrier per CPU core. On an 8-core instance, that is 8 carriers. Normally that is plenty, because a virtual thread waiting for the database unmounts and frees its carrier within microseconds. Here, each query in progress holds a carrier for its whole duration.

Suppose queries normally take 5 milliseconds, but under load some take 500 milliseconds. Eight slow queries at the same time are enough to pin all 8 carriers. For that half second, no other virtual thread can run at all, not even ones that only need a few microseconds of CPU to return a cached response. Throughput collapses toward 8 queries at a time, the CPU is mostly idle, and latency for everything else soars. With platform threads, the same 8 slow queries would have held 8 of 200 pool threads, and the other 192 would have kept working, which is why the old setup looked healthier.

Step throughEight slow queries pin all eight carriers
Virtual threadsCarriers (8)JDBC driverDatabase
8 requests mounted
enter synchronized execute()
send query, read reply
socket read while holding monitor: pinned
4,200 more requests: no free carrier
replies after 500 ms
carriers freed, the next 8 queries pin them again

7 messages in order. Play them, or step through them one at a time.

Pinned waits hold the carriers, so runnable virtual threads queue even though the CPU is idle.

3Confirming It With JFR

A thread dump is a snapshot; JFR shows how often it happens. The jdk.VirtualThreadPinned event is recorded whenever a virtual thread blocks while pinned for longer than a threshold, 20 milliseconds by default, and it includes the stack trace.

Shell
$ jcmd <pid> JFR.start duration=2m filename=pinned.jfr
$ jfr print --events jdk.VirtualThreadPinned pinned.jfr | grep -c VirtualThreadPinned
3172 <- pinned waits over 20 ms in two minutes
$ jfr summary pinned.jfr # or open in JDK Mission Control and group by top frame
# On JDK 21 to 23 only, a quick alternative:
java -Djdk.tracePinnedThreads=full -jar app.jar # prints a stack when a pinned thread blocks
# (this option was removed in JDK 24)

Grouping the events by stack shows whether the driver is the only source. Often there are a few others, such as a logging appender or a cache client, which is useful to know before choosing a fix.

4Option 1: Upgrade to JDK 24 or Later

On JDK 24 and later, JEP 491 lets a virtual thread hold a monitor and still unmount, so the socket read inside the driver's synchronized method no longer pins. The same driver, unchanged, now frees the carrier during the read. This fixes not only this driver but every other library with the same pattern, which is why it is the best long-term fix. JDK 25 is the LTS release to target. The cost is a JDK upgrade, with its own testing, which may not be possible this week.

5Option 2: Upgrade the Driver

Many JDBC drivers and other I/O libraries have released versions that replace synchronized with ReentrantLock in their connection and I/O paths specifically to avoid pinning on Java 21. A ReentrantLock is a java.util.concurrent lock, and blocking while holding one never pinned, so a virtual thread in such a driver unmounts during the socket read even on Java 21. Check the driver's release notes for virtual thread or Loom related changes and the minimum version that includes them. This is a smaller change than a JDK upgrade and fixes this incident, but other libraries on the request path may still pin, which the JFR grouping will show.

6Short-Term Mitigations

If neither upgrade can happen immediately, the simplest mitigation is to turn virtual threads off for this service, using spring.threads.virtual.enabled=false in Spring Boot, and go back to the pool that worked. If virtual threads must stay, two narrower options help. We can run the JDBC calls on a separate executor of platform threads, so pinning never involves carriers, at the cost of a hand-off per query. Or we can limit how many JDBC calls run at once with a semaphore, or a connection pool, whose size is safely below the carrier count, so some carriers always stay free for other work; for example with 8 carriers, at most 4 concurrent queries, which only works if that is enough database throughput. Raising jdk.virtualThreadScheduler.parallelism adds carriers and softens the stall, but it is a blunt tool that adds context switching and does not fix the cause.

Options and their costs
OptionFixesCost
Upgrade to JDK 24 or 25this driver and every similar librarya JDK upgrade and its testing
Upgrade the driverthis driver on Java 21small; other libraries may still pin
Virtual threads off for this servicethe incident, immediatelylose the benefits until fixed
Platform-thread executor for JDBCpinning on carriersa thread hand-off per query
Limit concurrent JDBC below carrier countkeeps some carriers freemay cap database throughput
Raise scheduler parallelismsoftens the stallblunt; the cause remains

7Follow-up questions

Why did the load test at low traffic look fine?

Because pinning only hurts when enough pinned waits overlap to occupy every carrier. At low traffic, a few pinned queries hold a few carriers briefly and the rest keep working. Load tests for virtual threads must include slow dependency responses and realistic concurrency.

Would a bigger connection pool help?

No, it would make it worse. More connections let more queries run at once, so more carriers get pinned. The limit that matters on Java 21 is the number of concurrent pinned calls compared with the number of carriers.

Does the scheduler compensate for pinned threads?

Not for a socket read inside synchronized on Java 21. Compensation, adding a temporary carrier, applies to some operations that capture the carrier, such as Object.wait() before JDK 24 and certain file I/O, but not to this kind of pinning. That is why every carrier could be stuck.

8Conclusion

In this answer, we read a thread dump in which every carrier was inside a JDBC driver's synchronized method doing a socket read, and explained that on Java 21 that combination pins the virtual thread, so the read blocks the carrier. With carriers equal to the CPU count, a handful of slow queries stalls every virtual thread. We confirmed it with the JFR jdk.VirtualThreadPinned event, noting that jdk.tracePinnedThreads was removed in JDK 24, and went through the options: upgrade to JDK 24 or later, upgrade the driver to one that uses ReentrantLock, and short-term mitigations such as turning virtual threads off or bounding concurrent JDBC calls.

What to say in the interviewAbout 90 seconds aloud

Open with

The thread dump tells the story. Every carrier is inside a synchronized method of the JDBC driver doing a socket read, and thousands of virtual threads are runnable but have no carrier.

Then, if they want more

On Java 21, a virtual thread can't unmount while it holds a monitor, so the read inside the synchronized method pins it, and the carrier's OS thread blocks for as long as the database takes.

With 8 cores we have 8 carriers, so 8 slow queries of half a second each are enough to stop every other virtual thread, even ones that just need microseconds of CPU, which is why throughput collapses while the CPU is idle. With platform threads, 8 slow queries only held 8 of 200 threads.

I'd confirm it with JFR's jdk.VirtualThreadPinned event, grouped by stack, to see whether the driver is the only source; on 21 you could also use jdk.tracePinnedThreads, but that was removed in 24. Then the options.

The best fix is upgrading to JDK 24 or later, ideally 25, where JEP 491 means synchronized no longer pins, so this and every similar library is fixed. The second is upgrading the driver to a version that uses ReentrantLock, which never pinned.

In the meantime, I'd turn virtual threads off for this service, or bound concurrent JDBC calls below the carrier count.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 24, Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JEP 444, Virtual Threads
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "On a Java 21 service, enabling virtual threads made throughput collapse under load. A...": https://scrivio-interview-library.netlify.app/java/virtual-threads/5368284d8b/