Java Thread suspend resume with boolean value

Description

Java Thread suspend resume with boolean value


class MyThread extends Thread {
  boolean active = true;

  public void Suspend() {
    active = false;//from   www  . j  ava  2s .  c  om
  }

  public synchronized void Resume() {
    active = true;
    notify();
  }

  public synchronized void run() {
    try {
      while (true) {
        if (active) {
          System.out.println("Running...");
          Thread.sleep(500);
        } else {
          System.out.println("Suspended...");
          wait();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

public class Main {
  public static void main(String args[]) throws Exception {
    MyThread mt = new MyThread();
    mt.start();
    while (true) {
      Thread.sleep(1000);
      mt.Suspend();
      Thread.sleep(1000);
      mt.Resume();
    }
  }
}



PreviousNext

Related