JatThread.java :  » Web-Framework » JAT » com » jat » core » util » Java Open Source

Java Open Source » Web Framework » JAT 
JAT » com » jat » core » util » JatThread.java
package com.jat.core.util;

/**
 * <p>Title: JAT</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
 * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
 * @author stf
 * @version 1.0
 * @since 1.2
 */

public abstract class JatThread extends Thread {

  public abstract void runIt();

  public JatThread(Runnable target) {
  super(target);
  }

  public JatThread(ThreadGroup group, Runnable target) {
  super(group, target);
  }

  public JatThread(String name) {
  super(name);
  }

  public JatThread(ThreadGroup group, String name) {
  super(group, name);
  }

  public JatThread(Runnable target, String name) {
  super(target, name);
  }

  public JatThread(ThreadGroup group, Runnable target, String name) {
  super(group, target, name);
  }

  public JatThread(ThreadGroup group, Runnable target, String name,
           long stackSize) {
  super(group, target, name, stackSize);
  }

  public void run() {
  Thread thisThread = Thread.currentThread();
  while(blinker==thisThread) {
    runIt();
    try {
    thisThread.sleep(this.interval);
    if(this.threadSuspended && blinker==thisThread) {
      synchronized(this) {
      while(this.threadSuspended) {
        wait();
      }
      }
    }
    } catch(InterruptedException ex) {
    }
  }
  this.destroy();
  }

  public long getInterval() {
  return this.interval;
  }

  public void setInterval(long interval) {
  this.interval = interval;
  }

  public void start() {
  blinker = new Thread(this, this.getName());
  blinker.start();
  }

  public final synchronized void safeStop() {
  Thread moribund = blinker;
  blinker = null;
  blinker.interrupt();
  notify();
  }

  public final synchronized void safeSuspend() {
  this.threadSuspended = true;
  }

  public final synchronized void safeResume() {
  this.threadSuspended = false;
  notify();
  }

  public void finalize() throws Throwable {
  this.destroy();
  super.finalize();
  }

  public void destroy() {
  this.safeStop();
  }

  private volatile Thread blinker;
  private long interval = DEFAULT_INTERVAL;
  private boolean threadSuspended = false;
  public final static long DEFAULT_INTERVAL = 1000*60*2;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.