I need to know what happens
Thanks in advance.
Interrupting a thread is a state-safe way to cancel it, but the thread itself has to be coded to pay attention to interrupts. Long, blocking Java operations that throw InterruptedException
will throw that exception if an .interrupt()
occurs while that thread is executing.
The .interrupt()
method sets the "interrupted" flag for that thread and interrupts any IO or sleep operations. It does nothing else, so it's up to your program to respond appropriately- and check its interrupt flag, via Thread.interrupted()
, at regular intervals.
If a thread doesn't check for interruptions, it cannot safely be stopped. Thread.stop()
is unsafe to use. So you use .interrupt()
to stop a thread, but when writing multithreaded code, it is up to you to make sure that .interrupt()
will do something sensible. This TechRepublic article is a pretty good tutorial.
One more important information worth sharing is that, there are two methods in Thread Class isInterrupted() and interrupted(). Latter one being a static method. isInterrupted() method call does not alter the state of interrupted attribute of Thread class, whereas interrupted() static method call can will set the value of interrupted boolean value to false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With