Virtual threads and structured concurrency
25 questions in 8 subtopics.
Virtual threads model
What is a virtual thread in Java 21+, how do you create one, and how does it differ from a platform thread?How long does this take to run, roughly, and how would the answer change withExecutors.newFixedThreadPool(200)? try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10_000; i++) ex.submit(() -> {
Thread.sleep(1000);
return null;
});
}
Carrier threads and mounting
Explain mounting and unmounting: what happens when a virtual thread calls a blocking socket read, and which threads are carriers?How is the virtual thread scheduler configured, and when would you change it?After moving to virtual threads, a library that usedThread.currentThread().getId() as a key to cache per-thread connections starts creating thousands of connections. Explain.Virtual threads took years of JDK work before becoming final in Java 21, because blocking operations across many APIs had to be changed to unmount, and some pinning remained until JDK 24. What does this cost tell you, and how should you sequence a multi-year adoption?Pinning and synchronized
What is pinning for virtual threads, what caused it before JDK 24, and what changed with JEP 491?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.Your platform runs on Java 21, and leadership wants to enable virtual threads across 60 services. Would you require an upgrade to Java 25 first? Lay out the trade-offs.Structured concurrency
Using only final APIs, write code that fetches a user and their orders in parallel on virtual threads and returns both, then explain what goes wrong if the user call fails quickly while the orders call is slow.What problem does structured concurrency (StructuredTaskScope) solve compared with submitting tasks to an ExecutorService, and what is its status in Java 25?A team wants to use StructuredTaskScope in production services on Java 25 with --enable-preview because it simplifies fan-out code. What is your decision and what conditions would you set?Scoped values
What are scoped values (ScopedValue, final in Java 25), and how do they differ from ThreadLocal?Your shared security library stores the authenticated principal in a ThreadLocal used by 50 services. Propose a migration path to ScopedValue and state its limitations.Virtual threads with Spring Boot
How do you enable virtual threads in Spring Boot, and what does the setting change?A Spring Boot application with virtual threads enabled uses@Scheduled tasks only and has no web server. It starts and then exits immediately. Explain and fix.After enabling spring.threads.virtual.enabled, a service sees many HikariCP connection timeout errors under the same traffic that worked before. Explain why and what you would change.You have 200 Spring Boot services on Java 17, some on Boot 2 and some on Boot 3. Plan the sequencing for adopting virtual threads over two years, including prerequisites and risks.When virtual threads do not help
In which cases do virtual threads not improve performance, and why?A team enabled virtual threads in an image-resizing service and saw no change in throughput, while CPU stays at 100%. Another team pooled virtual threads in a fixed pool of 50 and saw no gain either. Explain both.Virtual threads vs reactive
Compare virtual threads with reactive programming (for example Project Reactor and WebFlux) for a typical request-response service.You own 30 WebFlux services written over five years. Some engineers want to move them back to Spring MVC on virtual threads. How would you decide which, if any, to migrate?Java chose user-mode threads (Project Loom) rather than async/await keywords as C# and JavaScript did. Why, and what does that imply for language and framework bets in your organisation?As the technical leader of a large Java organisation in the virtual-thread era, what would you standardise or forbid to avoid the mistakes of the thread-pool era?No question matches these choices.