To determine whether a thread has been interrupted : Thread Stop « Thread « Java Tutorial






class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  public void run() {
    try {
      while (true) {
        System.out.print(firstName);
        sleep(aWhile);
        System.out.print(secondName + "\n");
      }
    } catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
}
public class MainClass {
  public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();
    second.start();
    third.start();
    try {
      Thread.sleep(3000);
    } catch (Exception e) {
      System.out.println(e);
    }
      first.interrupt();
      second.interrupt();
      third.interrupt();
    if (first.isInterrupted()) {
      System.out.println("First thread has been interrupted.");
    }
  }
}
A B C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
C a 
A a 
A b 
B a 
A c 
C b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A c 
C C c java.lang.InterruptedException: sleep interrupted
First thread has been interrupted.
B b java.lang.InterruptedException: sleep interrupted
A a java.lang.InterruptedException: sleep interrupted








10.4.Thread Stop
10.4.1.Stopping a Thread: Use boolean value to stop a thread
10.4.2.Stopping a Thread with interrupt()
10.4.3.Suspend, resume, and stop a thread.
10.4.4.To determine whether a thread has been interrupted