Executors and thread pools question 89 of 180 senior
What is work stealing, and which executors use it?
- Threads take tasks from each other's stacks at random
- Correct: Each worker has its own deque, pushes and pops at its own end, and steals from the other end of a busy worker's deque when idle
- The OS scheduler moves tasks between threads
- Only virtual threads use it
In a classic pool, all workers contend on one shared queue, and that queue is the bottleneck. In a work-stealing pool each worker owns a double-ended queue. It pushes and pops from its own end (LIFO, which keeps the freshest and most cache-warm task), and an idle worker steals from the opposite end (FIFO, taking the oldest and typically largest piece of work).
ForkJoinPool and the common pool behind parallel streams work this way. Executors.newWorkStealingPool
and the virtual thread scheduler steal the same way but run their own queue FIFO (asyncMode), which
suits event-style tasks that are never joined. It shines for recursive divide-and-conquer where tasks
spawn subtasks; it buys little for uniform, independent, blocking tasks, where a plain pool is simpler
and just as fast.
Quiz yourself on Executors and thread pools Suggest a correction