Since I can't comment, I'll post another answer. I just want to reinforce and clarify what Alexandr said. interrupt()
only sets a flag in the Thread
, and the extended Thread
or Runnable
object have to check if it have been interrupted with Thread.interrupted()
to do what it's supposed to do when interrupted.
interrupt()
also stops a wait()
or sleep()
, but pay attention that those methods UNSET the interrupt flag. So, if you call interrupt()
on a sleeping/waiting thread, it will stop sleeping/waiting but will not know that it was interrupted, except if you catch the exception, and will not continue to be interrupted.
I was able to fix a program of mine by implementing my own interrupt flag, which couldn't be overridden by the sleep()
and wait()
methods. Also, know that you can interrupt a thread from within with Thread.currentThread().interrupt()
.