Home »
Java programming language
Java - Difference Between interrupted() and isInterrupted() Methods
Java | interrupted() Vs. isInterrupted() methods: In this tutorial, we will learn about the interrupted() and isInterrupted() methods in Java and the differences between interrupted() and isInterrupted() methods.
By Preeti Jain Last updated : March 30, 2024
interrupted() Method
Example of interrupted() Method
// We will use Thread class methods so we are importing
// the package but it is not mandate because
// it is imported by default
import java.lang.Thread;
class InterruptedThread extends Thread {
// Overrides run() method of Thread class
public void run() {
for (int i = 0; i <= 3; ++i) {
// By using interrupted() method to check whether this
// thread has been interrupted or not it will return and
// execute the interrupted code
if (Thread.interrupted()) {
System.out.println("Is thread" + Thread.currentThread().getName() + " has been interrupted and status is set to " + " " + Thread.interrupted());
} else {
System.out.println("This thread has not been interrupted");
}
}
}
public static void main(String args[]) {
InterruptedThread it1 = new InterruptedThread();
InterruptedThread it2 = new InterruptedThread();
// By using start() method to call the run() method of
// Thread class and Thread class start() will call run()
// method of InterruptedThread class
it2.start();
it2.interrupt();
it1.start();
}
}
Output
E:\Programs>javac InterruptedThread.java
E:\Programs>java InterruptedThread
This thread has not been interrupted
This thread has not been interrupted
This thread has not been interrupted
Is thread Thread-1 has been interrupted: false
This thread has not been interrupted
This thread has not been interrupted
This thread has not been interrupted
This thread has not been interrupted
isInterrupted() Method
Example of isInterrupted() Method
// We will use Thread class methods so we are importing the package
// but it is not mandate because it is imported by default
import java.lang.Thread;
class InterruptedThread extends Thread {
// Overrides run() method of Thread class
public void run() {
for (int i = 0; i <= 3; ++i) {
// By using interrupted() method to check whether this thread
// has been interrupted or not it will return and execute
// the interrupted code
if (Thread.currentThread().isInterrupted()) {
System.out.println("Is the thread" + Thread.currentThread().getName() + "has been interrupted: " + Thread.currentThread().isInterrupted());
} else {
System.out.println("Is the thread" + Thread.currentThread().getName() + "has been interrupted: " + Thread.currentThread().isInterrupted());
}
}
}
public static void main(String args[]) {
InterruptedThread it1 = new InterruptedThread();
InterruptedThread it2 = new InterruptedThread();
// By using start() method to call the run() method of Thread class
// and Thread class start() will call run() method of
// InterruptedThread class
it2.start();
it2.interrupt();
it1.start();
}
}
Output
E:\Programs>javac InterruptedThread.java
E:\Programs>java InterruptedThread
Is the threadThread-1 has been interrupted: true
Is the threadThread-0 has been interrupted: false
Is the threadThread-1 has been interrupted: true
Is the threadThread-1 has been interrupted: true
Is the threadThread-0 has been interrupted: false
Is the threadThread-1 has been interrupted: true
Is the threadThread-0 has been interrupted: false
Is the threadThread-0 has been interrupted: false
Difference Between interrupted() and isInterrupted() Methods in Java
The following table shows the differences between interrupted() and isInterrupted() methods in Java:
interrupted() Method |
isInterrupted() Method |
The interrupted() method is a static method of the Thread class. |
The isInterrupted() method is an instance method of the Thread class. |
The interrupted() method clears the interrupted status of the current thread. |
The isInterrupted() method does not clear the interrupted status. |
The interrupted() method operates on the currently executing thread. |
The isInterrupted() method can be invoked on any Thread instance. |
The interrupted() method returns a boolean value indicating whether the thread was interrupted. |
The isInterrupted() method returns a boolean value indicating whether the thread is interrupted. |
The interrupted() method does not throw any checked exceptions. |
The isInterrupted() method does not throw any checked exceptions. |
The interrupted() method resets the interrupted status to false. |
The isInterrupted() method does not affect the interrupted status. |
The interrupted() method can be invoked without a thread reference. |
The isInterrupted() method requires a thread reference to invoke. |
The interrupted() method checks the interrupted status of the current thread. |
The isInterrupted() method checks the interrupted status of a specific thread. |
The interrupted() method provides a convenient way to check and clear the interrupted status. |
The isInterrupted() method offers a straightforward way to check the interrupted status without clearing it. |
The interrupted() method is useful when you want to reset the interrupted status after checking. |
The isInterrupted() method is useful when you want to check the interrupted status without affecting it. |