001: package com.jat.core.util;
002:
003: /**
004: * <p>Title: JAT</p>
005: * <p>Description: </p>
006: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
007: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
008: * @author stf
009: * @version 1.0
010: * @since 1.2
011: */
012:
013: public abstract class JatThread extends Thread {
014:
015: public abstract void runIt();
016:
017: public JatThread(Runnable target) {
018: super (target);
019: }
020:
021: public JatThread(ThreadGroup group, Runnable target) {
022: super (group, target);
023: }
024:
025: public JatThread(String name) {
026: super (name);
027: }
028:
029: public JatThread(ThreadGroup group, String name) {
030: super (group, name);
031: }
032:
033: public JatThread(Runnable target, String name) {
034: super (target, name);
035: }
036:
037: public JatThread(ThreadGroup group, Runnable target, String name) {
038: super (group, target, name);
039: }
040:
041: public JatThread(ThreadGroup group, Runnable target, String name,
042: long stackSize) {
043: super (group, target, name, stackSize);
044: }
045:
046: public void run() {
047: Thread this Thread = Thread.currentThread();
048: while (blinker == this Thread) {
049: runIt();
050: try {
051: this Thread.sleep(this .interval);
052: if (this .threadSuspended && blinker == this Thread) {
053: synchronized (this ) {
054: while (this .threadSuspended) {
055: wait();
056: }
057: }
058: }
059: } catch (InterruptedException ex) {
060: }
061: }
062: this .destroy();
063: }
064:
065: public long getInterval() {
066: return this .interval;
067: }
068:
069: public void setInterval(long interval) {
070: this .interval = interval;
071: }
072:
073: public void start() {
074: blinker = new Thread(this , this .getName());
075: blinker.start();
076: }
077:
078: public final synchronized void safeStop() {
079: Thread moribund = blinker;
080: blinker = null;
081: blinker.interrupt();
082: notify();
083: }
084:
085: public final synchronized void safeSuspend() {
086: this .threadSuspended = true;
087: }
088:
089: public final synchronized void safeResume() {
090: this .threadSuspended = false;
091: notify();
092: }
093:
094: public void finalize() throws Throwable {
095: this .destroy();
096: super .finalize();
097: }
098:
099: public void destroy() {
100: this .safeStop();
101: }
102:
103: private volatile Thread blinker;
104: private long interval = DEFAULT_INTERVAL;
105: private boolean threadSuspended = false;
106: public final static long DEFAULT_INTERVAL = 1000 * 60 * 2;
107: }
|