Home »
Java programming language
Differences between the interrupted() and isInterrupted() in Java
interrupted() and isInterrupted() in Java: Here, we are going to learn what are the differences between the interrupted() and isInterrupted() in Java programming language?
Submitted by Preeti Jain, on August 08, 2019
interrupted() and isInterrupted() in Java
Here, we will see how isInterrupted() differs from interrupted() in Java?
isInterrupted()
Example:
/* 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
Here, we will see how interrupted() differs from isInterrupted() in Java?
interrupted()
Example:
/* 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
TOP Interview Coding Problems/Challenges