Java - Difference Between the interrupted() and isInterrupted() Methods

Introduction

Thread class has a non-static isInterrupted() method returns if a thread has been interrupted.

When you call this method, unlike the interrupted() method, the interrupted status of the thread is not cleared.

Demo

public class Main {
  public static void main(String[] args) {
    // Check if the main thread is interrupted
    System.out.println("#1:" + Thread.interrupted());

    // Now interrupt the main thread
    Thread mainThread = Thread.currentThread();
    mainThread.interrupt();//from   www  .  j a  va 2s . c  o m

    // Check if it has been interrupted
    System.out.println("#2:" + mainThread.isInterrupted());

    // Check if it has been interrupted
    System.out.println("#3:" + mainThread.isInterrupted());

    System.out.println("#4:" + Thread.interrupted());

    System.out.println("#5:" + mainThread.isInterrupted());
  }
}

Result

Related Topic