Java / Java 21 Virtual Threads Interview questions
What is the difference between ExecutorService and StructuredTaskScope?
ExecutorService is a general-purpose task submission abstraction: tasks you submit can outlive the method that submitted them, and you're responsible for manually calling shutdown() and awaiting termination.
StructuredTaskScope is narrower and more disciplined by design - subtasks forked inside it are strictly bound to the enclosing try-with-resources block. When the block exits, every subtask is guaranteed to have completed, failed, or been cancelled; nothing can leak past it.
In short, ExecutorService gives flexibility at the cost of manual lifecycle management, while StructuredTaskScope trades some flexibility for automatic, composable lifecycle and error propagation.
More Related questions...