Quick revision
The short answer
In a ForkJoinPool, each worker has its own double-ended queue. A worker pushes the subtasks it forks onto one end and pops from that same end, last in first out, so it keeps working on the freshest, most related data, which is still in its cache. An idle worker steals from the other end of another worker's deque, first in first out, which usually gives it the oldest and largest piece of work. join() helps run other tasks instead of just blocking, and real blocking should go through ForkJoinPool.ManagedBlocker so the pool can compensate.
What to remember
In this answer, we saw how work stealing schedules a divide-and-conquer computation. Each worker pushes and pops its own subtasks at the top of its deque, LIFO, keeping related data in cache without locks, while idle workers steal from the base of other deques, FIFO, taking the oldest and largest tasks so a few steals balance the whole pool. join() runs or helps with other tasks instead of blocking, and blocking inside tasks starves the pool unless it goes through ForkJoinPool.ManagedBlocker so the pool can compensate.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In a ForkJoinPool, each worker has its own double-ended queue. A worker pushes the subtasks it forks onto one end and pops from that same end, last in first out, so it keeps working on the freshest, most related data, which is still in its cache. An idle worker steals from the other end of another worker's deque, first in first out, which usually gives it the oldest and largest piece of work. join() helps run other tasks instead of just blocking, and real blocking should go through ForkJoinPool.ManagedBlocker so the pool can compensate.
Why this question is asked
Work stealing is the idea that makes ForkJoinPool fast, and it is the reason the JDK uses that pool for parallel streams and the virtual thread scheduler. Many developers know the phrase but not the mechanics, and the mechanics explain both why divide-and-conquer code scales and why blocking inside a ForkJoinPool hurts. Interviewers ask this to see whether we understand the design rather than the API. In this answer, we will recap divide and conquer, look at the per-worker deque and its two ends, follow a steal step by step, see what join() really does, and finish with what blocking does to the pool and how ManagedBlocker helps.
1A Quick Recap of Divide and Conquer
Divide and conquer solves a big problem by splitting it into smaller problems of the same kind, solving those, and combining the results. To sum an array of 16 million numbers, we split it into two halves, split each half again, and so on until a piece has about 10,000 elements, which we sum with a plain loop. Then the partial sums are added back together on the way up.
This creates a tree of tasks. The top task is huge, the leaves are tiny, and there are thousands of them. A good scheduler for this tree has to do two things well: let one thread create and run many small tasks cheaply, and spread the big early branches across all cores quickly. Work stealing does both.
2Each Worker Has Its Own Deque
Every worker thread in a ForkJoinPool owns a deque, a double-ended queue of tasks. Think of the deque as having a top, where its owner works, and a base, where thieves take from. When a task running on worker W1 calls fork() on a subtask, the subtask is pushed onto the top of W1's deque. When W1 needs its next task, it pops from the top too. So the owner works last in, first out, like a stack.
The reason is locality. The task W1 forked most recently is the one closest to what it just did, usually the neighbouring half of the same small range of the array, and that data is still in W1's CPU cache. Working LIFO also means the owner goes deep into one branch of the tree before another, which keeps the number of tasks waiting in its deque small. And because only the owner touches the top, pushing and popping need no lock, just careful memory ordering.
| Position | Task | Size | Who takes it |
|---|---|---|---|
| base (oldest) | sum [8M, 16M) | 8 million | a thief, first |
| sum [4M, 8M) | 4 million | a thief, next | |
| sum [2M, 4M) | 2 million | a thief or W1 | |
| top (newest) | sum [1M, 2M) | 1 million | W1, next to run |
3How an Idle Worker Steals
When a worker's own deque is empty, it becomes a thief. It picks another worker's deque, effectively at random, and tries to take the task at the base, the oldest one. If that fails because another thief got there first, it tries elsewhere. Only when it cannot find any work does it park and wait to be woken.
Stealing from the base has two advantages. First, owner and thief work on opposite ends, so they rarely compete for the same task; a compare-and-set settles the rare case when only one task is left. Second, the oldest task in a divide-and-conquer deque is the biggest one, from high up in the tree, like the half [8M, 16M) above. A thief that steals it gets a large amount of work in one step, and it will split that work into its own subtasks in its own deque. So after a handful of steals, every core is busy with a large branch, and steals become rare.
6 messages in order. Play them, or step through them one at a time.
One steal gives W2 half the work; it then splits that half in its own deque.
4What join() Really Does
When a task calls join() on a subtask it forked, it needs that subtask's result. A naive implementation would block the thread until the result arrives, leaving a core idle. ForkJoinPool's join() avoids that. If the subtask is still on top of the worker's own deque, which is common when we fork one half and compute the other, the worker simply pops it and runs it itself. If the subtask was stolen, the worker can help the thief, running other tasks from that thief's deque or elsewhere, until the result is ready.
Only when there is nothing useful to run does the worker really wait, and in that case the pool may start or wake a spare thread to keep the target parallelism. This is why the usual coding pattern is left.fork(); right.compute(); left.join(): the current thread does real work on one half, and by the time it joins the other half, it either runs it itself or finds that a thief has already finished it.
5Why Blocking Inside Tasks Hurts, and ManagedBlocker
The pool has a target parallelism, normally one worker per core. It assumes its workers are either computing or looking for work. If a task blocks, for example in a JDBC call or Thread.sleep, its worker thread is stuck and the pool does not know. With 8 workers and 8 blocking tasks, the pool does nothing while work waits, even though the CPU is idle.
ForkJoinPool.ManagedBlocker is the way to tell the pool. We wrap the blocking operation in an object with two methods, block() and isReleasable(), and call ForkJoinPool.managedBlock(blocker). Before blocking, the pool may activate a spare thread to make up for the blocked one, and it retires it later.
Some JDK classes already do this internally, for example CompletableFuture's waiting methods and some locks when called from a ForkJoinPool worker. But ordinary blocking I/O does not, and compensation has limits, so the general advice stands: keep blocking work out of ForkJoinPool, and use ManagedBlocker only when a fork/join computation really has to wait.
6Follow-up questions
Why do owners use LIFO but thieves FIFO?
LIFO for the owner keeps it on fresh, cache-warm data and limits how many tasks pile up. FIFO for thieves gives them the oldest, biggest tasks, so one steal moves a lot of work, and it keeps owner and thief on opposite ends of the deque so they rarely contend.
Where do tasks submitted from outside the pool go?
A thread that is not a worker cannot push onto a worker's deque. Its tasks go into shared submission queues, which workers check and steal from like any other deque. Once the first task runs on a worker, its forked subtasks go into that worker's own deque.
What is asyncMode in a ForkJoinPool?
A constructor option that makes workers process their own local tasks in FIFO order instead of LIFO. It suits event-style tasks that are never joined, such as message handlers, and it is how the virtual thread scheduler's pool is configured.
7Conclusion
In this answer, we saw how work stealing schedules a divide-and-conquer computation. Each worker pushes and pops its own subtasks at the top of its deque, LIFO, keeping related data in cache without locks, while idle workers steal from the base of other deques, FIFO, taking the oldest and largest tasks so a few steals balance the whole pool. join() runs or helps with other tasks instead of blocking, and blocking inside tasks starves the pool unless it goes through ForkJoinPool.ManagedBlocker so the pool can compensate.
Open with
In a ForkJoinPool every worker thread has its own double-ended queue. When a task forks a subtask, it is pushed onto the top of the current worker's deque, and the worker also pops from the top, so it works last in first out.
Then, if they want more
That keeps it on the most recent and most related task, usually the neighbouring piece of the same data, which is still in its cache, and since only the owner uses that end, push and pop need no lock.
When a worker runs out of work, it steals from the other end, the base, of another worker's deque, first in first out.
In a divide-and-conquer computation the oldest task is the biggest one, like half of the whole array, so one steal gives the thief a lot of work, which it splits in its own deque, and after a few steals every core is busy and stealing becomes rare.
Owner and thief also work on opposite ends, so they rarely contend. join is not a plain blocking wait: if the forked task is still on top of our deque, we just run it ourselves, and if it was stolen we help by running other tasks until the result is ready.
That is why the pattern is fork one half, compute the other, then join. Blocking inside tasks is the weak spot, because the pool targets one worker per core and does not know a thread is stuck. ManagedBlocker tells the pool, so it can add a compensating thread.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does ForkJoinPool work stealing work, and why does it suit divide-and-conquer tasks?": https://scrivio-interview-library.netlify.app/java/executors/5a7c0719d9/