How to interrupt ExecutorService's threads
Solution 1
In order to do this, you need to submit()
a task to an ExecutorService
, rather than calling execute()
. When you do this, a Future
is returned that can be used to manipulate the scheduled task. In particular, you can call cancel(true)
on the associated Future
to interrupt a task that is currently executing (or skip execution altogether if the task hasn't started running yet).
By the way, the object returned by Executors.newSingleThreadExecutor()
is actually an ExecutorService
.
Solution 2
Another way to interrupt the executor's internally managed thread(s) is to call the shutdownNow(..)
method on your ExecutorService
. Note, however, that as opposed to @erickson's solution, this will result in the whole ThreadPoolExecutor
becoming unfit for further use.
I find this approach particularly useful in cases where the ExecutorService
is no longer needed and keeping tabs on the Future
instances is otherwise unnecessary (a prime example of this being the exit(..)
method of your application).
Relevant information from the ExecutorService#shutdownNow(..)
javadocs:
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt, so any task that fails to respond to interrupts may never terminate.
Comments
-
Ofek Ron almost 4 years
When using the
ExecutorService
returned byExecutors.newSingleThreadExecutor()
, how do I interrupt it? -
phil_20686 almost 10 yearsFor this to work, don't you have to manually make sure that the thread is interrupt-able. Not all threads can be interrupted. - I think anyway.
-
brady almost 10 yearsYes, this describes how to interrupt the thread running a task. Whether the task responds to interruption or not is a whole other question. That depends on what the task is doing.
-
john about 9 years@erickson I also have similar question here related to thread interruption. If possible, can you help me out over there. I haven't receive any response yet so any help will be greatly appreciated.
-
Wax almost 7 yearsYou have to check within your code that extends the Runnable class whether the thread was interrupted by doing
.isInterrupted()
to the current thread eg. Thread.currentThread.isInterrupted(), then you will need to throw anInterruptedException
. to complete the interruption. -
brady almost 7 years@Wax Not exactly. It's true that the task must be written to support interruptions. The more idiomatic way to check for interruption is to use the static method
Thread.interrupted()
, but you have to be aware that this "clears" the interrupted state flag, while the instance methodisInterrupted()
does not. However, there might be other, better ways to detect interruption, depending on what your task is doing. For instance, many blocking methods will instantly throw anInterruptedException
. If you detect interruption, you don't need to throw an exception; simply abort the task and return. -
Wax almost 7 years@erickson, sorry for bothering you. I am currently researching about how to interrupt threads and what I mentioned here is what I have tried so far. If I were to simply abort the task and return. In what way can I do this using ExecutorService? Thanks for your time.
-
krackoder over 5 yearsDoes
ExecutorService
handles interrupted threads? That is, if one of thefutures
managed byExecutorService
is cancelled withfuture.cancel(true)
, that threads interrupt flag will be true. DoesExecutorService
make sure that the interrupt flag of that thread is reset before that thread is re-used again for another task? -
brady over 5 years@nishant Yes, the interrupt status is cleared before the thread invokes another task