1. Introduction
A Java program with more than one thread finishes only when all non-daemon threads have finished execution or one of the threads call System.exit()
method. Sometimes, you may need to terminate a thread when you want to cancel the task that the thread is doing. When there is such requirement, we can interrupt the execution of the thread.
Java provides mechanism to interrupt the thread. Thread has to check if it has been interrupted or not and it can decide whether to respond to the interruption request or not. Thread can ignore the interruption request and continue with its execution.
2. Important methods
We’ll discuss three important methods related to interruption mechanism.
2.1 public void interrupt()
Interrupts the thread. If the thread is blocked in an invocation of wait()
, join()
or sleep()
methods (or any overloaded version of these methods), then the interrupt status is cleared and it will receive an InterruptedException
.
2.2 public static boolean interrupted()
This method tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. So if this method is called twice in succession, the second call would return false. If the thread is not alive at the time of calling this method, the call would be ignored and the method would return false.
When the thread is interrupted, we may throw InterruptedException
. This can be achieved using the following code:
if (Thread.interrupted()) // Clears interrupted status! throw new InterruptedException();
2.3 public boolean isInterrupted()
This methods tests whether the current thread has been interrupted. The interrupted status of the thread is unaffected by this method. If the thread is not alive at the time of calling this method, the call would be ignored and the method would return false.
3. What is the difference between interrupted() and isInterrupted() method
This is frequently asked question. There are two main differences between these two methods.
interrupted()
method is astatic
method whereasisInterrupted()
is not astatic
method.interrupted()
method clears the interrupted status whereasisInterrupted()
method has no effect on the interrupted status.
4. Java Thread interrupt(), interrupted() and isInterrupted () method example
In the following example, we are using interrupt()
method to interrupt the thread. We are then using isInterrupted()
method to check if the thread is interrupted. isInterrupted()
method is used to print the status of the thread.
package com.learnitweb; public class JavaIsInterruptedExample extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " printing value of i: " + i); if (Thread.interrupted()) { System.out.println(Thread.currentThread().getName() + " interrupted"); try { throw new InterruptedException(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String args[]) throws InterruptedException { JavaIsInterruptedExample thread1 = new JavaIsInterruptedExample(); JavaIsInterruptedExample thread2 = new JavaIsInterruptedExample(); thread1.setName("thread1"); thread2.setName("thread2"); thread1.start(); thread2.start(); System.out.println("is thread1 interrupted..: " + thread1.isInterrupted()); System.out.println("is thread2 interrupted..: " + thread2.isInterrupted()); // interrupt threads thread1.interrupt(); thread2.interrupt(); System.out.println("is thread1 interrupted..: " + thread1.isInterrupted()); System.out.println("is thread2 interrupted..: " + thread2.isInterrupted()); } }
Output
is thread1 interrupted..: false
is thread2 interrupted..: false
thread1 printing value of i: 1
thread1 interrupted
thread2 printing value of i: 1
thread2 interrupted
is thread1 interrupted..: true
is thread2 interrupted..: false
java.lang.InterruptedException
at com.learnitweb.JavaIsInterruptedExample.run(JavaIsInterruptedExample.java:9)
thread2 printing value of i: 2
thread2 printing value of i: 3
java.lang.InterruptedException
thread2 printing value of i: 4
at com.learnitweb.JavaIsInterruptedExample.run(JavaIsInterruptedExample.java:9)thread2 printing value of i: 5
thread1 printing value of i: 2
thread1 printing value of i: 3
thread1 printing value of i: 4
thread1 printing value of i: 5