Home » Java programming language

Java Thread Class boolean isInterrupted() method with Example

Java Thread Class boolean isInterrupted() method: Here, we are going to learn about the boolean isInterrupted() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 24, 2019

Thread Class boolean isInterrupted()

  • This method is available in package java.lang.Thread.isInterrupted().
  • This method is used to check the thread, whether a thread has been interrupted or not.
  • This method is not static so we cannot access this method with a class name too.
  • The return type of this method is boolean so it returns true if the thread has been interrupted and else returns false if the thread has not been interrupted.
  • We need to remember that this method returns true if the thread has been interrupted and then does not set the flag to false like as interrupted() method.
  • This method raises an exception.

Syntax:

    boolean isInterrupted(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is boolean, it returns true or false and if the thread has been interrupted so it returns true and else returns false.

Java program to demonstrate 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 thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-0 has been interrupted: false



Comments and Discussions!

Load comments ↻






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