[concurrency-interest] Is cancel method used anywhere?
Tue, 13 Jul 2004 16:56:14 -0400
> I need to create a Thread Pool with a limited number of threads which
> queues up tasks to run and can be shutdown in a way that will allow
> running tasks to be cancelled gracefully. Everything in
> java.util.concurrent seemed to exactly what I needed until I noticed
> that the shutdownNow method does not use the cancel method to stop my
> tasks that will implement the Future interface. Is there an easy way to
> do this?
ThreadPoolExecutor uses Thread.interrupt() to attempt to stop running tasks,
as does FutureTask.cancel(true). Unless you are using a specialized Executor
with custom Future implementations, you should probably make sure that your
tasks check for interruption at likely cancellation points (but not _too_ often).
From the javadocs for shutdownNow:
"There are no guarantees beyond best-effort attempts to stop processing
actively executing tasks. For example, typical implementations will cancel
via Thread.interrupt(), so if any tasks mask or fail to respond to interrupts,
they may never terminate."