Virtual Threads (Project Loom)
Give a real production-style example of using Virtual Threads (Project Loom). Walk through the scenario end-to-end.
Answers use simple, clear English.
Audio N/AQuick interview answer
Scenario: HTTP gateway handles 10k concurrent upstream calls each on its own virtual thread. Implementation notes: Use structured concurrency; prefer ReentrantLock over synchronized in hot paths.
Detailed answer
Scenario: HTTP gateway handles 10k concurrent upstream calls each on its own virtual thread. Implementation notes: 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: Mid-level.
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