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

  • This method is available in java.lang package.
  • This is a static method so this method is accessible with the class name too.
  • This method is used to check whether a thread has been interrupted or not interrupted and then set interrupted flag status.
  • The return type of this method is boolean so it returns true if the thread has been interrupted else return false.
  • In case of the interrupted() method, we need to notice that this method returns true if the thread has been interrupted and then after interrupted flag or boolean variable is set to false else returns false.
  • The syntax of this method is given below:
    public static boolean interrupted(){
    }

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

  • This method is available in java.lang package.
  • This is the non-static method so this method is accessible with the class object.
  • This method is used to check whether a thread has been interrupted or not interrupted.
  • The return type of this method is boolean so it returns true if the thread has been interrupted else return false.
  • In case of isInterrupted() method, we need to notice that this method returns true if the thread has been interrupted and then after interrupting it does not again set the boolean variable as false like as interrupted() method else returns false.
  • The syntax of this method is given below:
    public boolean isInterrupted(){
    }

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.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.