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.concurrent structures
  • java nio (but not java.io) , not use interruptedexception, instead using closedbyinterruptexception.

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

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -