Sub topic 3
Pinning and Safe Virtual-Thread Rollouts
Assume Java 21+, Spring Boot 3.2+, and a Spring MVC application using blocking JDBC or HTTP calls.
Pinning: when a virtual thread cannot unmount
A virtual thread normally unmounts when it blocks. Its carrier thread can then run another virtual thread. Pinning happens when the virtual thread cannot unmount, so it holds the carrier thread during the blocking operation.
Common causes include:
- Blocking inside a
synchronizedblock or method. - Blocking while executing native code.
Prefer ReentrantLock when a locked section can perform blocking work:
import java.util.concurrent.locks.ReentrantLock;
class Counter {
private final ReentrantLock lock = new ReentrantLock();
private long value;
long increment() {
lock.lock();
try {
return ++value;
} finally {
lock.unlock();
}
}
}Keep the locked section short. A lock prevents races, but it does not make a slow database or network call faster.
ThreadLocal and request-scoped state
A ThreadLocal stores a value associated with one thread. With platform threads, a small pool limits the number of stored values. Virtual threads can create very large numbers of thread-local maps.
Review code that stores large objects, credentials, or request data in ThreadLocal. Remove values in a finally block when the value should not live for the whole request. Spring request-scoped beans are managed separately, but code should still avoid assuming that one platform thread represents the request.
Running concurrent subtasks with virtual threads
Use one virtual thread per blocking task for simple fan-out:
try (var executor = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) {
var user = executor.submit(this::fetchUser);
var orders = executor.submit(this::fetchOrders);
return user.get() + " / " + orders.get();
}The executor supplies concurrency, not unlimited downstream capacity. Use a Semaphore to cap database or service calls.
Seeing virtual threads
Enable pinning traces during investigation:
java -Djdk.tracePinnedThreads=full -jar app.jar
jcmd <pid> Thread.dump_to_file -format=json threads.jsonInspect the JSON dump for virtual-thread names and states. Use Java Flight Recorder to examine virtual-thread start, end, mount, unmount, and pinning events. Compare blocked time, CPU time, request latency, and downstream saturation.
Virtual threads or reactive?
Choose virtual threads when blocking code is clearer and the workload waits on I/O. Reactive programming remains useful for streaming, explicit backpressure, and ecosystems built around nonblocking APIs. Do not adopt both models in one call path without a clear reason.
A checklist for a safe rollout
- Use Java 21 or newer and enable
spring.threads.virtual.enabled=true. - Test JDBC pools and downstream rate limits.
- Search for blocking
synchronizedsections and native calls. - Review
ThreadLocalmemory use. - Compare latency, CPU, memory, and pinning before and after rollout.
- Release gradually with rollback ready.
Exercise
Why does a Semaphore remain useful when virtual threads are cheap?
Show answer
It limits access to scarce resources, such as database connections, even when many virtual threads can wait concurrently.