interrupted() method
interrupted() is a static method in Thread class that determines if the current thread has been interrupted.
"The interrupted status of the thread is cleared by this method".
The interrupted() methodis always checks the current thread and clears the interruption "flag".
Therefore, 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() method
isInterrupted() is an instance method that tests if this thread instance has been interrupted.
It reports the status of the thread on which it is invoked.
"The interrupted status of the thread is unaffected by this method", it does not clear the interruption flag. If the flag is set, it will remain set after calling this method.
Use it here
If you were writing your own thread pool, you might want to check the interrupted status on one of the threads that you are managing. In that case, you would call managedThread.isInterrupted() to check it's interrupted status.
If you are writing your own InterruptedException handlers that don't immediately retrigger an equivalent exception via Thread.currentThread(). interrupt()
(for example, you might have a finally block after your exception 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