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


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.


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   w  ww  .ja  v a 2  s  . co 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());


Related Topic