Java/Threads/Template

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Inner self run threads

public class InnerSelfRunMain extends Object {
  private Thread internalThread;
  private volatile boolean noStopRequested;
  public InnerSelfRunMain() {
    // add your code here ...
    System.out.println("initializing...");
    noStopRequested = true;
    Runnable r = new Runnable() {
      public void run() {
        try {
          runWork();
        } catch (Exception x) {
          // in case ANY exception slips through
          x.printStackTrace();
        }
      }
    };
    internalThread = new Thread(r);
    internalThread.start();
  }
  private void runWork() {
    while (noStopRequested) {
      System.out.println("in runWork() - still going...");
      try {
        Thread.sleep(700);
      } catch (InterruptedException x) {
        Thread.currentThread().interrupt(); // re-assert interrupt
      }
    }
  }
  public void stopRequest() {
    noStopRequested = false;
    internalThread.interrupt();
  }
  public boolean isAlive() {
    return internalThread.isAlive();
  }
  public static void main(String[] args) {
    InnerSelfRunMain sr = new InnerSelfRunMain();
    try {
      Thread.sleep(3000);
    } catch (InterruptedException x) {
    }
    sr.stopRequest();
  }
}





Self run thread

public class SelfRunThreadTemplate extends Object implements Runnable {
  private Thread internalThread;
  private volatile boolean noStopRequested;
  public SelfRunThreadTemplate() {
    // add your code here ...
    System.out.println("initializing...");
    noStopRequested = true;
    internalThread = new Thread(this);
    internalThread.start();
  }
  public void run() {
    if ( Thread.currentThread() != internalThread ) {
      throw new RuntimeException("only the internal " +
        "thread is allowed to invoke run()");
    }
    while ( noStopRequested ) {
      System.out.println("in run() - still going...");
      try {
        Thread.sleep(700);
      } catch ( InterruptedException x ) {
        Thread.currentThread().interrupt(); 
      }
    }
  }
  public void stopRequest() {
    noStopRequested = false;
    internalThread.interrupt();
  }
  public boolean isAlive() {
    return internalThread.isAlive();
  }
  public static void main(String[] args) {
    SelfRunThreadTemplate sr = new SelfRunThreadTemplate();
    try { Thread.sleep(3000); } catch ( InterruptedException x ) { }
    sr.stopRequest();
  }
}