Friday, 8 April 2016

How interrupting a thread works ?


Thread provides :
  • A boolean flag : which holds interrupted status
  • interrupt method : for interrupting a thread and for querying whether a thread has been interrupted.
    Interrupting a thread sets the interrupted status.

When thread A interrupts thread B, A is like requesting that B stop what it is doing when it gets to a convenient stopping point

Handling interruption
You must have a plan for responding to interruption :
  • Propagate the InterruptedException
    OR
  • Restore the interrupt : Catch InterruptedException and restore the interrupted status by calling interrupt on the current thread.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.