Quick revision
The short answer
CompletableFuture.cancel(true) just completes the future with a CancellationException; the mayInterruptIfRunning flag has no effect, and the computation that would have produced the value keeps running, because the future has no link to any thread. FutureTask.cancel(true), which is what ExecutorService.submit returns, does interrupt the thread running the task. In both cases, stopping real work needs the work itself to cooperate, by reacting to interrupts or by being cancelled at its source.
What to remember
In this answer, we saw that a CompletableFuture is only a holder for a result, so cancel(true) completes it with CancellationException, ignores the mayInterruptIfRunning flag, and leaves the computation running; its dependent stages complete exceptionally. A FutureTask from ExecutorService.submit knows its running thread, so cancel(true) interrupts it, but the work stops only if it reacts to interrupts. Either way, stopping real work needs cooperation from the work or from the underlying API, such as aborting the HTTP request.
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.
CompletableFuture.cancel(true) just completes the future with a CancellationException; the mayInterruptIfRunning flag has no effect, and the computation that would have produced the value keeps running, because the future has no link to any thread. FutureTask.cancel(true), which is what ExecutorService.submit returns, does interrupt the thread running the task. In both cases, stopping real work needs the work itself to cooperate, by reacting to interrupts or by being cancelled at its source.
Why this question is asked
Cancellation sounds like a way to save resources: the user left, so we cancel and stop the work. With CompletableFuture that assumption is wrong, and services that rely on it keep doing work nobody needs. Interviewers ask this to see whether we understand what a future really is, a holder for a result, and how that differs from a task. In this answer, we will recap the two kinds of future, see what cancel does on each, look at how dependent stages behave, and then see what we must do to stop the actual work.
1A Quick Recap: A Result Holder Versus a Task
Both classes implement Future, the Java 5 interface with get, isDone and cancel. But they are built very differently. A FutureTask wraps a piece of code. When an executor runs it, the FutureTask records which thread is running it, and it completes itself with the code's result. So it knows about the work and the thread doing it.
A CompletableFuture is only a holder for a result. Anyone can complete it, and the code that will do so might be a supplyAsync task, an HTTP client callback or a message listener. The future does not know which thread, if any, is producing its value. Even supplyAsync creates the CompletableFuture and a separate task object that completes it later; the future keeps no reference back to that task's thread.
2What cancel(true) Does on a CompletableFuture
Calling cancel on a CompletableFuture is the same as completing it exceptionally with a CancellationException. The Javadoc says plainly that the mayInterruptIfRunning flag has no effect, because interrupts are not used to control processing. Let us watch the work keep going.
Notice that cancel returned true and isCancelled reports true, so from the caller's point of view the future is cancelled. But the loop runs all five steps, using a pool thread for the full five seconds. When it finishes, it tries to complete the future, finds it already completed, and its result is thrown away.
3What cancel(true) Does on a FutureTask
ExecutorService.submit returns a Future that is a FutureTask in the standard executors. Its cancel works differently. If the task has not started, it will never start. If it is running and we pass true, the FutureTask calls interrupt() on the thread running it.
| Aspect | CompletableFuture | FutureTask (from submit) |
|---|---|---|
| Future state | completed with CancellationException | completed with CancellationException |
mayInterruptIfRunning | ignored | interrupts the running thread |
| Task not started yet | supplyAsync skips it; other producers still run | never runs |
| Running work | keeps running to the end | stops only if it reacts to interrupts |
| Dependent stages | complete exceptionally | no stages: plain Future |
FutureTask is linked to the thread doing the work.So here the work really stops, but only because Thread.sleep responds to interrupts. If the task were a tight loop that never checks Thread.currentThread().isInterrupted(), or a blocking socket read that ignores interrupts, the interrupt would be set and nothing would happen. An interrupt is a polite request, not a forced stop.
4How Dependent Stages Behave
Cancelling a CompletableFuture affects every stage built on it. Each dependent stage completes exceptionally too, so the cancellation flows down the chain like any other failure. The dependent stages see a CompletionException whose cause is the CancellationException.
But cancellation does not travel upward. Cancelling text, a dependent stage, does nothing to f or to the work behind it. This is one more reason why cancelling a future near the end of a chain saves nothing.
5Stopping the Real Work
Because neither future can force work to stop, real cancellation has to be built into the work. For our own loops, we check the interrupted flag or a shared cancelled flag between steps. For remote calls, we cancel at the source: many HTTP clients can abort an in-flight request, and we can hook that up by listening for the cancellation.
Whether the inner cancel really aborts the connection depends on the client and its version, so check your client's documentation. For fan-out inside one service, structured concurrency links subtasks to a scope and interrupts them when the scope is cancelled; it is a preview feature in Java 25, so check its status in your JDK.
6Follow-up questions
Why did the JDK designers make CompletableFuture ignore the interrupt flag?
Because a CompletableFuture may be completed by any code, and often by no thread that we own, such as an I/O callback. There is no single thread to interrupt. Interrupting whichever thread happened to be running a stage could hurt unrelated work, for example a shared I/O thread.
What does join() throw on a cancelled future compared with its dependents?
On the cancelled future itself, join() throws CancellationException directly. On a dependent stage, join() throws CompletionException with the CancellationException as its cause. get() on the cancelled future also throws CancellationException.
Is cancelling a CompletableFuture useless then?
No. It tells waiting callers and dependent stages to stop waiting, which frees a request thread and lets us respond quickly. It just does not free the resources of the computation, so we must also cancel the work itself when that matters.
7Conclusion
In this answer, we saw that a CompletableFuture is only a holder for a result, so cancel(true) completes it with CancellationException, ignores the mayInterruptIfRunning flag, and leaves the computation running; its dependent stages complete exceptionally. A FutureTask from ExecutorService.submit knows its running thread, so cancel(true) interrupts it, but the work stops only if it reacts to interrupts. Either way, stopping real work needs cooperation from the work or from the underlying API, such as aborting the HTTP request.
Open with
On a CompletableFuture, cancel just completes the future with a CancellationException. The mayInterruptIfRunning flag is documented as having no effect. The reason is that a CompletableFuture is only a holder for a result; it has no link to the thread or task producing the value, so it has nothing to interrupt.
Then, if they want more
The computation keeps running to the end, and when it tries to complete the future, its result is ignored. Any dependent stages complete exceptionally, with a CompletionException whose cause is the CancellationException, but cancellation does not travel back up the chain.
A FutureTask, which is what submit returns from the standard executors, wraps the task itself and records the running thread. So cancel(true) on it interrupts that thread, and if the task has not started, it never runs.
Even then, the work only stops if it reacts to the interrupt, for example by being in sleep or a blocking queue call, or by checking the interrupted flag.
So in both cases, if I want real cancellation, I build it into the work: check for interruption in loops, and cancel at the source for remote calls, like aborting the HTTP request, or use structured concurrency once it is final in my JDK.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 5
- 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 "What does cancel(true) do on a CompletableFuture, and how does that differ from a...": https://scrivio-interview-library.netlify.app/java/completable-future/22f571d8c1/