multithreading - What does java.lang.Thread.interrupt() do? -
could explain java.lang.thread.interrupt() when invoked?
thread.interrupt() sets interrupted status/flag of target thread. code running in target thread may poll interrupted status , handle appropriately. methods block such object.wait() may consume interrupted status , throw appropriate exception (usually interruptedexception)
interruption in java not pre-emptive. put way both threads have cooperate in order process interrupt properly. if target thread not poll interrupted status interrupt ignored.
polling occurs via thread.interrupted() method returns current thread's interrupted status , clears interrupt flag. thread might such throw interruptedexception.
edit (from thilo comments): api methods have built in interrupt handling. of top of head includes.
object.wait()/thread.sleep()- most
java.util.concurrentstructures - java nio (but not java.io) , not use
interruptedexception, instead usingclosedbyinterruptexception.
edit (from @thomas-pornin's answer exactly same question completeness)
thread interruption gentle way nudge thread. used give threads chance exit cleanly, opposed thread.stop() more shooting thread assault rifle.
Comments
Post a Comment