Java Thread interrupt check

Description

Java Thread interrupt check

class Timer extends Thread {
  public void run() {
    while (true) {
      System.out.println("Timer running. Date & time: " + new java.util.Date());
      if (Thread.interrupted()) {
        System.out.println("Timer was interrupted");
        return;//  w w  w .  j ava2  s .  c  o m
      }
    }
  }
}

public class Main {
  public static void main(String args[]) throws InterruptedException {
    Timer t = new Timer();
    t.start();
    Thread.sleep(20);
    t.interrupt();
  }
}



PreviousNext

Related