Friday, 22 April 2016

interrupted() vs. isInterrupted() methods


interrupted
()
  • Static method in Thread class
  • It determines if the current thread has been interrupted
  • It always checks the current thread and clears the interruption flag.
If a thread was interrupted, calling interrupted() once would return true, while a second call to it would return false until the current thread is interrupted again.

isInterrupted()
  • Instance method that tests if this thread instance has been interrupted.
  • It reports the status of the thread on which it is invoked.
  • It does not change the interruption flag.


USE CASES
  • When writing your own thread pool, To check the interrupted status on one of the threads that you are managing, you can call thread1.isInterrupted() to check it's interrupted status.
  • When writing your own InterruptedException handlers, you might want to check whether that thread that you are currently running on has been interrupted via an outside call or InterruptedException. In that case, you would check the boolean value of Thread.interrupted() to check on the status of your current thread.

No comments:

Post a Comment

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