Title: CompletableFuture: 45-Second Call Starved Pool | TheCodeForge
Open Graph Title: CompletableFuture: 45-Second Call Starved Pool | TheCodeForge
Description: A 45-second call starves all ForkJoinPool threads, causing future.join() to hang. Avoid I/O in ForkJoinPool—set timeouts, use dedicated executor.
Open Graph Description: A 45-second call starves all ForkJoinPool threads, causing future.join() to hang. Avoid I/O in ForkJoinPool—set timeouts, use dedicated executor.
Opengraph URL: https://thecodeforge.io/java/java-completablefuture/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/java-completablefuture/"
},
"headline": "CompletableFuture: 45-Second Call Starved Pool",
"description": "A 45-second call starves all ForkJoinPool threads, causing future.join() to hang. Avoid I/O in ForkJoinPool—set timeouts, use dedicated executor.",
"keywords": "CompletableFuture: 45-Second Call Starved Pool, Java, Advanced, Concurrency, A 45-second call starves all ForkJoinPool threads, causing future.join() to hang. Avoid I/O in ForkJoinPool—set timeouts, use dedicated executor.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-09T00:00:00+00:00",
"dateModified": "2026-09-26T00:00:00+00:00",
"url": "https://thecodeforge.io/java/java-completablefuture/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "CompletableFuture: 45-Second Call Starved Pool", "item": "https://thecodeforge.io/java/java-completablefuture/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between thenApply() and thenCompose()?",
"acceptedAnswer": {
"@type": "Answer",
"text": "thenApply() transforms the result of a completed stage — it takes a T and returns a U, wrapping the result in CompletableFuture. thenCompose() does the same transformation but flattens the result, so you get CompletableFuture instead of CompletableFuture>. Rule of thumb: if your mapping function returns a plain value, use thenApply(). If it returns another CompletableFuture, use thenCompose(). Mixing them up is the most common chaining mistake I see in code reviews."
}
},
{
"@type": "Question",
"name": "Is CompletableFuture thread-safe?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes. CompletableFuture is designed for concurrent use. The complete(), completeExceptionally(), and all callback registration methods (thenApply, thenAccept, etc.) are thread-safe. Multiple threads can register callbacks on the same CompletableFuture simultaneously, and the JVM guarantees that only one completion takes effect — the first call to complete() wins. That said, the code inside your lambda callbacks is your responsibility. If your thenApply mutates shared state without synchronization, that's a bug in your code, not in CompletableFuture."
}
},
{
"@type": "Question",
"name": "Can you cancel a CompletableFuture? What actually happens?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Calling cancel(true) on a CompletableFuture sets it to completed exceptionally with CancellationException. If the underlying task is still running, cancel(true) also calls Thread.interrupt() on the worker thread. However, interruption is cooperative — your code must check Thread.currentThread().isInterrupted() or handle InterruptedException to actually stop. If your task ignores interruption, it keeps running even after cancel() returns true. cancel(false) only works if the task hasn't started yet. In practice, reliable cancellation requires designing your tasks to respond to interruption signals."
}
},
{
"@type": "Question",
"name": "When should I use CompletableFuture vs Project Reactor Mono/Flux?",
"acceptedAnswer": {
"@type": "Answer",
"text": "CompletableFuture is ideal for orchestrating a small number of discrete async operations — combining 3-5 service calls, chaining dependent tasks, or adding async behavior to otherwise synchronous code. Project Reactor (and RxJava) are better for streaming data, backpressure handling, and complex reactive pipelines with many stages. If you're already in the Spring WebFlux ecosystem with WebClient, you'll naturally use Mono/Flux. If you're in a traditional Spring MVC app and need to parallelize a few calls, CompletableFuture is simpler and doesn't require learning the entire reactive type system."
}
},
{
"@type": "Question",
"name": "What happens to CompletableFuture threads when the JVM shuts down?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The default common ForkJoinPool uses daemon threads. Daemon threads are terminated immediately when the JVM exits — they don't get a chance to finish their work. If you have a CompletableFuture mid-execution during shutdown, that work is silently abandoned. No exception is thrown, no warning is logged. For critical background work (payment processing, data persistence), use a managed ExecutorService with non-daemon threads and register a shutdown hook that calls shutdown() followed by awaitTermination() to give in-flight tasks time to complete."
}
},
{
"@type": "Question",
"name": "How do you test code that uses CompletableFuture?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The most reliable approach is dependency-injecting your Executor into the async method, then passing Runnable::run (a direct executor) in tests. This runs all tasks synchronously on the test thread, eliminating race conditions and flakiness. For error testing, use CompletableFuture.failedFuture() to simulate failures without mocking entire services. For timeout testing, use orTimeout(1, SECONDS) and assert that a CompletionException wrapping a TimeoutException is thrown. Avoid Thread.sleep() in tests — it makes them slow and still doesn't guarantee the async work has completed."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large