How to interrupt ExecutorService's threads

32,485

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.

Share:
32,485
Ofek Ron
Author by

Ofek Ron

What can i say i like to code

Updated on June 16, 2020

Comments

  • Ofek Ron
    Ofek Ron almost 4 years

    When using the ExecutorService returned by Executors.newSingleThreadExecutor(), how do I interrupt it?

  • phil_20686
    phil_20686 almost 10 years
    For 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
    brady almost 10 years
    Yes, 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
    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
    Wax almost 7 years
    You 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 an InterruptedException. to complete the interruption.
  • brady
    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 method isInterrupted() 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 an InterruptedException. If you detect interruption, you don't need to throw an exception; simply abort the task and return.
  • Wax
    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
    krackoder over 5 years
    Does ExecutorService handles interrupted threads? That is, if one of the futures managed by ExecutorService is cancelled with future.cancel(true), that threads interrupt flag will be true. Does ExecutorService make sure that the interrupt flag of that thread is reset before that thread is re-used again for another task?
  • brady
    brady over 5 years
    @nishant Yes, the interrupt status is cleared before the thread invokes another task