An interrupt is a cooperative mechanism for a thread to signal another thread that it should, at its convenience and if it feels like it, stop what it is doing and do something else
A thread is said to be interrupted, if its interrupt status is set
It’s up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate
The only way (I know) to interrupt thread is by calling thread.interrupt() instance method from one thread to the another thread
For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption
Interruptible methods
These methods generally poll for interruption before blocking or doing any work, so as to be as responsive to interruption as possible
They throw InterruptedException checked exception
Examples:
Thread.sleep()
BlockingQueue.put()
Thread.join()
When they throw exception, do they control interrupt flag of current thread?
Interrupt flag
aka interrupt status
Indication to a thread that it should stop what it is doing and do something else
It is common for the thread to terminate
By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so.
The idea is that an interrupt should be handled once
However, it’s always possible that interrupt status will immediately be set again, by another thread invoking interrupt
Following method throws Interrupted exception when current thread is interrupted and clears
Interruption related Methods
thread.isInterrupted()
instance method
Tests if this thread has been interrupted
thread.interrupt()
instance method
set the interrupt flag, hence interrupting the thread
It does not stop the execution of the flow
Used for example, if a thread is sleeping then interrupting the thread will cause the sleep to be interrupted and throw InterruptedException
Similar for join
Only one sleep can be interrupted by one interrupt
Can be called as Thread.currentThread().interrupt()
Thread.interrupted()
static method
Tests if the current thread has been interrupted ANDclear the interrupted flag
Handling of interrupt is done generally by polling via this method which returns the current thread’s interrupted status AND clears that interrupt flag.
Usually the thread might then do something such as throw InterruptedException
Since interrupt status is cleared automatically, then it is not need to be done
As soon as the thread is interrupted, we want to clear the interrupt status flag and decide what to do with the interrupt. This helps to check other interrupt status in the future
Setting the flag can ONLY be done using thread.interrupt()
Clearing the flag can ONLY be done explicitly using Thread.interrupted() testing method which is used to poll for thread interruption
Implicitly some methods which throw InterruptedException clears the interrupt status as well for the current thread. For example:
Thread.sleep
Object.wait
Thread.join
try { Thread.sleep(1000);} catch (InterruptedException e) { // interrupt flag is already cleared here // because Thread.sleep caught an interrupt for the current thread // and threw exception after clearning the interrupt internally}
Throwing InterruptedException indicates to the thread that thread is interrupted
If you cannot throw exception (for example you are implementing Runnable), then you should restore the interrupt status by calling Thread.currentThread().interrupt()
General Pattern for throwing exception
if (Thread.interrupted()) // Clears interrupted status! throw new InterruptedException();