ForkJoinPool.ManagedBlocker describes a wait that might block a fork/join worker. The pool may compensate with another worker while that wait is in progress.
Java ManagedBlocker: declare a rare blocking wait to the pool
Operational contract
A fork/join worker that waits on a queue can starve unrelated tasks if all workers do the same. The blocker checks for a ready item without waiting, then takes an item only when necessary. The pool may activate a spare worker; this is not an unlimited capacity guarantee and does not turn arbitrary I/O into CPU work. For sustained network or disk waits, a separate bounded executor is usually easier to account for. The queue producer must also have a termination or cancellation policy so the consumer cannot wait forever.
Failure case
A parallel audit occasionally waits for one receipt to arrive in a handoff queue. The wait is rare, so a ManagedBlocker can tell the pool that one worker is parked. If all 47,000 receipts require remote fetches, that workload needs an I/O concurrency limit instead of 47,000 blockers.
Java code
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ForkJoinPool;
public class ReceiptQueueBlocker implements ForkJoinPool.ManagedBlocker {
private final BlockingQueue<String> receipts;
private String receipt;
public ReceiptQueueBlocker(BlockingQueue<String> receipts) {
this.receipts = receipts;
}
@Override public boolean isReleasable() {
return receipt != null || (receipt = receipts.poll()) != null;
}
@Override public boolean block() throws InterruptedException {
if (!isReleasable()) receipt = receipts.take();
return true;
}
public String await() throws InterruptedException {
ForkJoinPool.managedBlock(this);
return receipt;
}
}Performance and ownership cost
A successful poll is O(1) queue work in typical implementations; a miss can wait indefinitely unless an outer cancellation policy interrupts it. Compensation can create extra workers and memory pressure. Bound both queue size and the number of waits.
Common Mistakes
- Do not expect managedBlock to create unlimited workers.
- Do not use it to hide sustained blocking I/O inside a CPU pool.
- Do not leave an unbounded take without a producer or cancellation contract.
Connected lessons
- Java ForkJoinPool: own a custom pool and its shutdown
- Java ExecutorService: bounded admission and shutdown
- Java cancellation: timed waits and cooperative interruption
- Java RecursiveTask: split work only above a measured threshold
- Java fork/join failures: one failed task does not roll back sibling work
- Java parallel streams: reduce values instead of mutating shared results
- Java HTTP and fork/join decisions quiz
- Advanced Java
