Video in progress
What is thread starvation deadlock? What happens when a task in a pool submits a new task to the same pool and waits for its result?
All threads in the pool are busy waiting for the results of inner tasks, and the inner tasks sit in the queue waiting for a free thread, so nobody moves forward. The JVM doesn't report this as a deadlock, but the service freezes completely.
📖 Answer
A small example:
ExecutorService pool = Executors.newFixedThreadPool(4);
Future<Report> outer = pool.submit(() -> {
Future<Data> a = pool.submit(this::loadA); // the very same pool
Future<Data> b = pool.submit(this::loadB);
return merge(a.get(), b.get()); // the thread is busy, waiting
});
If 4 outer tasks arrive at the same time, 4 threads sit in a.get(), and the loadA/loadB tasks are in the queue. Under low load everything works. The problem only appears at a peak, which is why it's hard to find in a test.
In a thread dump: all the pool's threads are WAITING inside FutureTask.get, and there's no "Found deadlock".
The same happens with CompletableFuture: if you call .join() inside a task on commonPool and the child task is also on commonPool. ForkJoinPool sometimes rescues you by starting a compensating thread, but you can't rely on that.
Solutions, in order of preference:
- Don't block. Compose instead of waiting for the result:
a.thenCombine(b, this::merge). No thread waits. - Separate the pools by stage. Outer tasks in one pool, inner IO in another. The dependency graph must have no cycles.
- Virtual threads. Each task gets its own thread, and the "no free thread" situation disappears. But you still need to set the downstream limit separately.
- A bounded wait:
get(timeout). This turns the deadlock into an error, but it doesn't fix the root cause.
In a large system I keep a small map for each executor: "who submits to it, and what does that task wait for". Libraries create their own pools too (HTTP client, Kafka, Hikari), and the cycle often appears on the boundary between teams, where nobody sees the whole picture.
Sign up to mark studied questions and save your progress.