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()

  • 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:

/*  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()

  • 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:

/*  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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.