When you must perform blocking work inside a ForkJoinPool, how does ManagedBlocker prevent thread starvation?
answer
- Pool keeps ~N active workers; blocking silently drops that → starvation
- ManagedBlocker: block() does the wait, isReleasable() cheap pre-check
- Invoke via ForkJoinPool.managedBlock(blocker)
- Past parallelism only when it could stall
- Mitigation only — sustained blocking → dedicated pool / virtual threads
basics
~20 sForkJoinPool.ManagedBlocker lets a task warn the pool before blocking, so the pool can wake an idle worker or start a thread. By default it grows past its parallelism only if no other unblocked worker would remain: progress is guaranteed, not full parallelism.
solid answer
~50 sForkJoinPool sizes itself to a parallelism level (by default the core count). A worker that blocks on I/O or a lock holds one of those threads, and enough such blocks can stall the pool, especially the shared common pool. ManagedBlocker is the controlled escape hatch: you implement block(), which does the waiting and returns true when no more blocking is needed, and isReleasable(), a quick check of whether blocking is needed at all, then call ForkJoinPool.managedBlock(blocker). Before the worker blocks, the pool may wake an idle worker or start a thread, and it restores its count afterwards. In JDK 25 and 27 the default policy (minimumRunnable 1) grows the pool past its parallelism only when it could otherwise run out of unblocked workers; a larger minimumRunnable keeps more threads running. It is a mitigation for occasional waits: sustained blocking belongs on a dedicated executor or virtual threads.
go deeper
Aware that blocking on a ForkJoinPool is problematic and that there's a special mechanism to do it more safely, without needing the details.
Knows ManagedBlocker lets the pool wake or add a thread so blocked workers cannot stall it, and that it is invoked via ForkJoinPool.managedBlock.
Can explain the block()/isReleasable() contract, when the pool grows past its parallelism (by default only near a stall, capped by maximumPoolSize), and that it is a mitigation rather than a green light for heavy blocking.
Reasons about when occasional bounded blocking inside a CPU-bound Fork/Join computation justifies ManagedBlocker, decides whether the default policy, which grows past parallelism only for liveness, or a higher minimumRunnable fits, measures thread growth under load, and otherwise routes blocking to dedicated executors or virtual threads.
## The starvation problem restated A **`ForkJoinPool`** is sized to a **parallelism** level — by default the number of CPU cores. The whole design assumes tasks are short and **non-blocking**, so a busy worker is either computing or stealing, never parked mid-task. If a worker **blocks** (network call, lock, `Thread.sleep`, waiting on another future), it holds a thread but does no work. Block enough workers and the pool's effective concurrency drops toward zero — **thread starvation**. On the **common pool** this is worse because unrelated parallel streams and CompletableFutures share it. The class Javadoc says the pool adjusts its threads for tasks stalled waiting to join others, but "no such adjustments are guaranteed in the face of blocked I/O or other unmanaged synchronization". ## ManagedBlocker: telling the pool you're about to block Sometimes blocking inside Fork/Join is unavoidable. **`ForkJoinPool.ManagedBlocker`** is the official cooperative mechanism for this. It's a small interface with two methods: - **`boolean block() throws InterruptedException`** — perform the actual blocking operation here; return `true` when no further blocking is needed. - **`boolean isReleasable()`** — a quick check that returns `true` if blocking is currently unnecessary (e.g. the resource is already available). The Javadoc requires it to tolerate repeated calls, and the pool calls it before it compensates, so it should not block itself. You don't call `block()` directly. Instead you call the static **`ForkJoinPool.managedBlock(blocker)`**. That method drives the protocol: it repeatedly checks `isReleasable()` and calls `block()` until either reports it's done. Off a pool worker, that loop is all it does. ## How it prevents starvation: compensation On a pool worker, once `isReleasable()` says the wait is real, the pool may **compensate** before `block()` runs, the first way that applies: 1. **Wakes an idle worker** that is parked waiting for work, if there is one. 2. **Lets its count of active workers drop by one**, adding no thread, if at full size with enough other workers still running. 3. **Starts a thread** otherwise, up to the pool's maximum size: one it lacked while below its parallelism, or a spare beyond it. When `block()` returns, the pool restores its count. What matters is *when* it grows. In JDK 25 and 27 the pool by default does not hold the full parallelism target: at full size, it adds a spare only when it could stall for lack of any unblocked worker. The knob is the constructor's `minimumRunnable` parameter, whose default of 1 "ensures liveness". So a default parallelism-4 pool already at four threads, with one worker parked in `managedBlock` while three others compute, stays at four, three of them running, even with tasks queued; built with `minimumRunnable` 4, it starts a fifth thread instead. The implementation's own notes give the reason: most blockages are transient, and replacing them eagerly causes longer-term oversubscription. Compensation is also **bounded**. A pool's `maximumPoolSize` caps its threads; the common pool allows 256 spares by default (the `java.util.concurrent.ForkJoinPool.common.maximumSpares` property). When a needed replacement would exceed the cap, the pool throws `RejectedExecutionException`, unless the constructor's `saturate` predicate returns `true` and lets it run short. Several JDK synchronisers call `managedBlock` themselves when the waiting thread is a pool worker: `CompletableFuture.get()`/`join()`, `Phaser`, the untimed waits of `LinkedTransferQueue`, and `Condition.await()` (so `LinkedBlockingQueue.take()`). Most timed waits park without it. ## The protocol shape ``` // the shape of ForkJoinPool.managedBlock(blocker) on a pool worker while (!blocker.isReleasable()) { adjusted = pool.compensate(); // wake an idle worker, let the active count drop, // or start a thread; may throw RejectedExecutionException try { done = blocker.block(); // the real wait; true = no more blocking needed } finally { if (adjusted) pool.restoreActiveCount(); } if (done) break; } ``` (The real methods are private and also handle retries and shutdown.) ## What it does and does not give you - **Does:** ensure, up to the thread cap, that a managed wait cannot leave the pool with no runnable worker, avoiding starvation or deadlock of a shared pool across an occasional, bounded block. - **Does not:** keep full parallelism by default, make blocking cheap, or turn Fork/Join into a good I/O pool. Each spare is a real platform thread; under sustained blocking they pile up toward the cap, and once the blocked threads wake, the pool briefly runs more threads than cores. - **Tuning knob:** if occasional blocking measurably costs throughput, raise `minimumRunnable` through the long-form constructor (Java 9+); its Javadoc warns a larger value "might improve throughput in the presence of blocked activities, but might not". For **sustained** blocking workloads, the right answer is a **dedicated, generously-sized executor** or **virtual threads** (Java 21+), which are purpose-built to block cheaply. ## When a principal reaches for it You use ManagedBlocker when a CPU-bound Fork/Join computation has *unavoidable, occasional* blocking points you can't move off the pool — say, a parallel algorithm that sometimes waits on a bounded cache or a rate limiter. You confirm the blocking is bounded, wrap it, decide whether the default `minimumRunnable` is enough, and watch the thread count under load. ## Mental model A ManagedBlocker is a cook raising a hand before stepping away to wait by the phone. An idle cook gets waved in and an empty stove gets a cook; with every stove staffed, the kitchen runs one short, and a temp comes only when nobody would be left cooking. A cook who walks off *without* raising a hand is never noticed, so even an empty kitchen gets no temp.
- What is the purpose of isReleasable() versus block()?isReleasable() is a cheap, non-blocking check the pool calls first to see whether blocking is even necessary (e.g. the resource is already available), so it can skip blocking and the compensation machinery. block() performs the actual blocking wait and returns true once complete.
- Why isn't ManagedBlocker a substitute for using virtual threads for I/O-heavy work?Each spare is a real, costly platform thread, the pool grows past its parallelism only when it could otherwise stall, and it stops at its cap (256 spares on the common pool by default), so sustained blocking either piles up threads or still loses throughput. Virtual threads are cheap and unmount from carriers when they block, scaling to huge numbers of blocking operations — the right tool for I/O-bound concurrency, leaving Fork/Join for CPU-bound recursion.
- One of your workers called managedBlock, yet the pool started no new thread. Why?Either it woke an idle worker instead, or it was already at full size. In JDK 25 and 27, once a pool has as many threads as its parallelism, it just counts one fewer active worker while others still run, adding a spare only when too few unblocked workers would remain (default minimumRunnable 1); below full size it would have started one. A larger minimumRunnable (Java 9+) keeps more running, which the Javadoc says might or might not help throughput.
- Calling block() directly instead of via ForkJoinPool.managedBlock
- Treating ManagedBlocker as making blocking cheap — each compensation thread is a real OS thread
- Implementing isReleasable() so that it blocks — the pool calls it repeatedly, before compensating
- Using ManagedBlocker for heavy sustained I/O instead of a dedicated executor or virtual threads
- Expecting a spare thread for every managed block — at full size the pool adds one only near a stall
- Assuming compensation is unlimited — spares stop at maximumPoolSize (256 spares on the common pool)
- ForkJoinPool.ManagedBlocker
- Java 1.7
- ForkJoinPool constructor with minimumRunnable
- Java 9
Compensation policy checked against JDK 27 and identical at JDK 25 LTS: by default (minimumRunnable 1) the pool wakes an idle worker or starts one up to its parallelism, and grows past its parallelism only when it could otherwise stall.
context
- ForkJoinPool.ManagedBlocker (Java SE 27 & JDK 27)docs.oracle.com
- ForkJoinPool (Java SE 27 & JDK 27)docs.oracle.com
- ForkJoinPool.java at jdk-27-gagithub.com