Prev Next

Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions

Which is better for high-throughput I/O - platform or virtual threads, and why?

For workloads dominated by blocking I/O, such as a typical web service making blocking database or HTTP calls per request, virtual threads are the better fit in Java 21.

Platform threads map one-to-one to OS threads, which are expensive to create and hold significant memory for their stacks (often megabytes each), so a thread-per-request model on platform threads realistically caps out at a few thousand concurrent requests before the OS and JVM overhead of managing that many threads dominates. This is why traditional servers instead used a limited platform-thread pool combined with either asynchronous, callback-based, or reactive code to avoid tying up a scarce thread on every blocking call.

// blocking style, but scales like async, thanks to virtual threads
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -> {
        var result = blockingDbCall();   // carrier freed while this blocks
        respond(result);
    });
}

Virtual threads let you write the same simple, blocking-style, thread-per-request code, but because a blocked virtual thread unmounts from its carrier instead of tying it up, a small number of carrier platform threads can support hundreds of thousands of concurrently blocked virtual threads at once. The result is I/O-bound throughput that rivals async/reactive approaches, without giving up straightforward, debuggable, synchronous-looking code.

Platform threads still make sense for long-running, CPU-bound work, where there's no blocking to unmount from anyway and virtual threads offer no advantage, or when interacting with code that pins frequently, such as legacy synchronized-heavy libraries doing I/O.

Why do platform threads struggle to scale to very high concurrent blocking I/O?
For which kind of workload do virtual threads offer little to no advantage?

More Related questions...

What is a thread in Java? What is multithreading in Java? What are the different states of a thread in Java? How do you create a thread in Java? What is the Runnable interface used for? What is a daemon thread in Java? What is thread priority in Java? What is the purpose of the synchronized keyword? What is the purpose of the volatile keyword? What are the types of locks available in java.util.concurrent.locks? Define a race condition in multithreading? What is the Executor framework in Java? What is a virtual thread in Java 21? What is a thread pool? Describe the purpose of the java.util.concurrent package? How does the synchronized keyword achieve mutual exclusion internally? Why should you prefer ReentrantLock over synchronized in some cases? What is the difference between wait() and sleep()? What is the difference between notify() and notifyAll()? How does ConcurrentHashMap achieve thread safety? What is the difference between ConcurrentHashMap and Hashtable? Why should you use CompletableFuture instead of Future? How does the ForkJoinPool execute tasks? What is the difference between Runnable and Callable? How do you handle thread interruption in Java? What happens when a deadlock occurs in a multithreaded application? When should you use CountDownLatch instead of CyclicBarrier? How does compare-and-swap (CAS) work in atomic classes? What is the difference between ExecutorService shutdown() and shutdownNow()? How is the Java Memory Model relevant to concurrent programming? How does ThreadLocal work internally? What is the difference between CopyOnWriteArrayList and a synchronized ArrayList? When would you choose a fixed thread pool over a cached thread pool? How does a BlockingQueue support the producer-consumer pattern? What happens when you call start() twice on the same thread? Explain the lifecycle of a virtual thread in Java 21? Explain the internal working of structured concurrency in Java 21? Why does a synchronized block pin a virtual thread to its carrier thread? What is the difference between ScopedValue and ThreadLocal? Explain the internal working of the ForkJoinPool work-stealing algorithm? How can you optimize code to avoid false sharing? Why does LongAdder outperform AtomicLong under high contention? Why does the ABA problem occur in lock-free CAS-based algorithms? Explain the execution flow of a parallel stream operation? How do you troubleshoot a deadlock in a production Java application? What is the difference between StampedLock and ReadWriteLock? Which is better for high-throughput I/O - platform or virtual threads, and why? How can you optimize a thread pool for mixed CPU-bound and I/O-bound workloads? Explain the internal working of AbstractQueuedSynchronizer (AQS)? Why doesn't increasing the thread pool size always improve throughput?
Show more question and Answers...


Comments & Discussions