Spring Cancel Async Task
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring's @Async makes it easy to run work on a background executor, but cancellation is only possible if you keep a handle to the running task and the task cooperates. In other words, cancelling a Spring async task is not just a framework switch; it depends on the return type, the executor, and whether the code responds to interruption.
Return a Future or CompletableFuture
If an @Async method returns void, the caller has no direct cancellation handle. To cancel work, return Future or CompletableFuture.
Now the caller can keep the returned future and request cancellation.
Cancelling from the Caller
Once you have the future, call cancel(true).
The true flag means interruption is requested for the executing thread when possible. But that does not guarantee the task stops immediately. The task code must either block in an interruptible call such as Thread.sleep() or explicitly check interruption state.
Configure an Executor You Control
Spring @Async uses an executor. Define one explicitly so task behavior is predictable.
With a dedicated executor, you can reason more clearly about queueing, shutdown, and interruption behavior.
Cancellation Is Cooperative
This is the most important concept. Calling cancel(true) does not forcibly kill arbitrary Java code. If the task is CPU-bound and never checks interruption, it may continue running.
For long loops, check interruption explicitly:
For blocking I/O or external calls, cancellation may depend on the underlying client library. Some operations are interruptible, some are not.
Tracking Many Async Jobs
If users can start and cancel several tasks, keep a registry keyed by job ID.
This pattern is useful for REST APIs where one request starts work and another request cancels it later.
Common Pitfalls
The biggest pitfall is trying to cancel an @Async method that returns void. Without a future handle, you have no standard way to request cancellation for that invocation.
Another mistake is assuming cancel(true) forcefully stops all work. It only signals interruption. If the code ignores interruption, the task may continue.
Developers also sometimes use CompletableFuture.completedFuture(...) incorrectly for genuinely asynchronous work. If the heavy work has already happened before the future is returned, cancellation will not help.
Finally, remember that cancellation and cleanup belong together. If a task owns files, connections, or partial database state, interruption handling should leave the system consistent.
Summary
- To cancel Spring
@Asyncwork, returnFutureorCompletableFuture, notvoid. - Keep the future and call
cancel(true)from the caller. - Cancellation is cooperative, so task code must react to interruption.
- Use a dedicated executor for predictable async behavior.
- For user-cancellable jobs, store futures in a registry keyed by task or job ID.
Related reading
- Spring Kafka asynchronous send calls block
- Spring Kafka is Acknowledgement.acknowledge thread safe?
- spring kafka thorws InstanceAlreadyExistsException exception after setting concurrency > 1
- Spring @KafkaListener and concurrency
- Spring catch all route for index.html
- Spring Cloud - SQS - The specified queue does not exist for this wsdl version
- Spring MVC and Async
- Spring Non-blocking Rest Send and forget

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.