In case of futures representing jobs of the commonly used ThreadPoolExecutor
, cancelled tasks are not immediately removed from the queue, as the method purge()
indicates:
purge()
Tries to remove from the work queue all Future
tasks that have been cancelled. This method can be useful as a
storage reclamation operation, that has no other impact on
functionality. Cancelled tasks are never executed, but may
accumulate in work queues until worker threads can actively
remove them. Invoking this method instead tries to remove them now.
However, this method may fail to remove tasks in
the presence of interference by other threads.
In case of CompletableFuture
, it’s not said explicitly, but since the CompletableFuture
operates on the Executor
abstraction and has no control over the implementation at all, we can assume that it won’t remove cancelled jobs from any queue. But if the prerequisites were not fulfilled at the point of cancellation (i.e. when you use asyncJob.thenApplyAsy(…, someExecutor)
and cancel it before asyncJob
has been completed), the job might not get enqueued in the first place.