Java Reference
In-Depth Information
Listing 7.9. Interrupting a Task in a Dedicated Thread.
ExecutorService.submit returns a Future describing the task. Future has a
cancel method that takes a boolean argument, mayInterruptIfRunning , and returns
a value indicating whether the cancellation attempt was successful. (This tells you only
whether it was able to deliver the interruption, not whether the task detected and acted on
it.) When mayInterruptIfRunning is true and the task is currently running in some
thread, then that thread is interrupted. Setting this argument to false means “don't run this
task if it hasn't started yet”, and should be used for tasks that are not designed to handle in-
terruption.
Since you shouldn't interrupt a thread unless you know its interruption policy, when is it OK
to call cancel with an argument of true ? The task execution threads created by the stand-
ard Executor implementations implement an interruption policy that lets tasks be cancelled
Search WWH ::




Custom Search