Virtual Threads (Project Loom)
What common mistakes do candidates make around Virtual Threads (Project Loom), and how do you avoid them?
Answers use simple, clear English.
Quick interview answer
Common mistakes: Using platform thread pool sizes tuned for virtual threads; thread-local abuse. Best practices: Use structured concurrency; prefer ReentrantLock over synchronized in hot paths.
Detailed answer
Common mistakes: Using platform thread pool sizes tuned for virtual threads; thread-local abuse. Best practices: Use structured concurrency; prefer ReentrantLock over synchronized in hot paths. Core: Virtual threads are lightweight; block on IO without pinning platform thread (mostly). Executors.newVirtualThreadPerTaskExecutor() simplifies massive concurrency. Real-time example: HTTP gateway handles 10k concurrent upstream calls each on its own virtual thread. Pros: Simpler than reactive stacks for IO-bound code; familiar thread model. Cons: Pinning with synchronized/native/JDBC drivers still possible; not for CPU-bound. Common mistakes: Using platform thread pool sizes tuned for virtual threads; thread-local abuse. Best practices: Use structured concurrency; prefer ReentrantLock over synchronized in hot paths. Audience level: Junior.
Full explanation
Virtual threads are lightweight; block on IO without pinning platform thread (mostly). Executors.newVirtualThreadPerTaskExecutor() simplifies massive concurrency.
Real example & use case
HTTP gateway handles 10k concurrent upstream calls each on its own virtual thread.
Pros & cons
Pros: Simpler than reactive stacks for IO-bound code; familiar thread model. Cons: Pinning with synchronized/native/JDBC drivers still possible; not for CPU-bound.
Common mistakes
Using platform thread pool sizes tuned for virtual threads; thread-local abuse.
Best practices
Use structured concurrency; prefer ReentrantLock over synchronized in hot paths.
Follow-up questions
Open one as its own read / solve / listen card