Virtual Threads (Project Loom)
What are the key trade-offs of Virtual Threads (Project Loom)? When would you choose it vs alternatives?
Answers use simple, clear English.
Audio N/AQuick interview answer
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.
Detailed answer
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. Decision: pick when benefits outweigh operational cost. 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: Architect.
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