Stopping a Thread: set a boolean variable that the thread checks occasionally : Wait Notify « Thread « Java Tutorial






public class Main {
  public static void main(String[] argv) throws Exception {
    MyThread thread = new MyThread();
    thread.start();

    thread.stop = true;
  }
}

class MyThread extends Thread {
  boolean stop = false;

  public void run() {
    while (true) {
      if (stop) {
        return;
      }
    }
  }
}








10.21.Wait Notify
10.21.1.Use wait() and notify() from Object class
10.21.2.Monitor a thread's status.
10.21.3.Stopping a Thread: set a boolean variable that the thread checks occasionally
10.21.4.Pausing a Thread: set boolean a variable that the thread checks occasionally