Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

What does cancel(true) do on a CompletableFuture, and how does that differ from a FutureTask returned by ExecutorService.submit?

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.

Java
CompletableFuture<Report> f = CompletableFuture.supplyAsync(() -> {
for (int i = 1; i <= 5; i++) { sleepQuietly(1000); log.info("step {}", i); }
return new Report();
}, pool);
Thread.sleep(1500);
System.out.println(f.cancel(true)); // true
System.out.println(f.isCancelled()); // true
f.join(); // throws CancellationException
// The log keeps printing: step 2, step 3, step 4, step 5
// The task completes, tries to set its result, and the value is ignored

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.

Java
Future<Report> t = pool.submit(() -> {
for (int i = 1; i <= 5; i++) { Thread.sleep(1000); log.info("step {}", i); }
return new Report();
});
Thread.sleep(1500);
System.out.println(t.cancel(true)); // true, and the worker thread is interrupted
// step 1 was logged; Thread.sleep in step 2 throws InterruptedException and the task ends
t.get(); // throws CancellationException
cancel(true) on the two kinds of future
AspectCompletableFutureFutureTask (from submit)
Future statecompleted with CancellationExceptioncompleted with CancellationException
mayInterruptIfRunningignoredinterrupts the running thread
Task not started yetsupplyAsync skips it; other producers still runnever runs
Running workkeeps running to the endstops only if it reacts to interrupts
Dependent stagescomplete exceptionallyno stages: plain Future
Only 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.

Java
CompletableFuture<String> text = f.thenApply(Report::render);
f.cancel(true);
text.isCompletedExceptionally(); // true
text.join(); // CompletionException, cause: CancellationException
text.isCancelled(); // false: only f itself was cancelled

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.

Java
CompletableFuture<HttpResponse<String>> inFlight = httpClient.sendAsync(request, BodyHandlers.ofString());
CompletableFuture<HttpResponse<String>> forCallers = inFlight.copy(); // what we hand out
forCallers.whenComplete((r, ex) -> {
if (forCallers.isCancelled()) inFlight.cancel(true); // forward the cancel to the client's own future
});
return forCallers;

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.

What to say in the interviewAbout 80 seconds aloud

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.
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/