Any thread running in Java language can be interrupted by calling the interrupt method. Note that the interrupt method is different from stop or terminate methods which have been deprecated now in JDK. To better understand the interrupt method, we need to look at the code of Thread class in Java which has the following native method defined in it:

private native void interrupt0();

The purpose of above method is to set the interrupt flag of the thread on which it is invoked. When we call interrupt method on a thread then the interrupt0 method is automatically invoked from this method.

interrupted() and isInterrupted() methods

Once the interrupt status has been set for a thread, it can check for the same by calling isInterrupted or interrupted method. Both these methods have different meaning as described below:

The interrupted method clears the interrupt flag on the thread and then returns true or false depending upon the status of the thread. On the other hand, the isInterrupted method returns the interrupt status but doesn’t clear it. Moreover, the interrupted method is a static method and isInterrupted method is an instance method. The interrupted method internally invokes the isInterrupted method. The Thread class code for both these method used for checking the status of thread is:

    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

As stated above, this method is calling the overloaded version of isInterrupted method.

    public boolean isInterrupted() {
        return isInterrupted(false);
    }
    
    private native boolean isInterrupted(boolean ClearInterrupted)

The isInterrupted method is overloaded in the Thread class and the 1 argument version is a native method.

Use of thread interruption

Invoking the interrupt method on a thread doesn’t stop thread but is a notification to the thread we need its attention. When the interrupt method is invoked on a thread then the flag us set which can be checked by using the two methods described above. A while loop which constantly checks for interrupt status looks like:

while(!Thread.currentThread().isInterrupted()){
........
........
}

At first, it looks as if this mechanism will interrupt the execution of a thread but in fact it should be used when task management is required.

HandlingInterruptedExcetpion

If a thread is in blocked state because of of wait or sleep method and interrupt method is called on it then it will result in InterruptedException being raised. This is the only reason that when sleep or wait method is invoked, we need to catch or specify InterruptedException. Note that InterruptedException is a checked exception. When such a exception is to be expected then we have two options:
1) Specify the InterruptedException by using throws keyword. We don’t need catch block in this case.
2) Use the catch block which looks like:

try{
    sleep()
} catch(InterruptedException e) {
    Thread.currentThread().interrupt();
}

In the above code, we are setting the interrupt status of current thread and hence leaving the handling responsibility to the thread.

Interrupting threads in Java admin Core Java
Any thread running in Java language can be interrupted by calling the interrupt method. Note that the interrupt method is different from stop or terminate methods which have been deprecated now in JDK. To better understand the interrupt method, we need to look at the code of Thread class...
Any thread running in Java language can be interrupted by calling the interrupt method. Note that the interrupt method is different from stop or terminate methods which have been deprecated now in JDK. To better understand the interrupt method, we need to look at the code of Thread class in Java which has the following native method defined in it: <code>private native void interrupt0();</code> The purpose of above method is to set the <strong>interrupt flag</strong> of the thread on which it is invoked. When we call interrupt method on a thread then the interrupt0 method is automatically invoked from this method. <h2>interrupted() and isInterrupted() methods</h2> Once the interrupt status has been set for a thread, it can check for the same by calling isInterrupted or interrupted method. Both these methods have different meaning as described below: The <strong>interrupted method clears the interrupt flag</strong> on the thread and then returns true or false depending upon the status of the thread. On the other hand, the isInterrupted method returns the interrupt status but doesn't clear it. Moreover, the <strong>interrupted method is a static method</strong> and <strong>isInterrupted method is an instance method</strong>. The interrupted method internally invokes the isInterrupted method. The Thread class code for both these method used for checking the status of thread is: 1 As stated above, this method is calling the overloaded version of isInterrupted method. 1 The isInterrupted method is overloaded in the Thread class and the 1 argument version is a native method. <h2>Use of thread interruption</h2> Invoking the interrupt method on a thread doesn't stop thread but is a notification to the thread we need its attention. When the interrupt method is invoked on a thread then the flag us set which can be checked by using the two methods described above. A while loop which constantly checks for interrupt status looks like: 1 At first, it looks as if this mechanism will interrupt the execution of a thread but in fact it should be used when task management is required. <h2>HandlingInterruptedExcetpion</h2> If a thread is in blocked state because of of wait or sleep method and interrupt method is called on it then it will result in InterruptedException being raised. This is the only reason that when sleep or wait method is invoked, we need to catch or specify InterruptedException. Note that InterruptedException is a <a href="http://www.javaexperience.com/checked-and-un-checked-exceptions-in-java/" title="Checked and Un-Checked exceptions in Java">checked exception</a>. When such a exception is to be expected then we have two options: 1) Specify the InterruptedException by using throws keyword. We don't need catch block in this case. 2) Use the catch block which looks like: 1 In the above code, we are setting the interrupt status of current thread and hence leaving the handling responsibility to the thread.