+ private static Log c_logger = LogFactory.getLog(ethread.class);
+
+ // the actual java thread object.
+ private volatile Thread c_RealThread = null;
+ // provides synchronization for the thread.
+ private volatile Object c_lock = new Object();
+ // this is the timing period, for a timed thread. if zero, then this is a single shot thread.
+ private long c_period = 0;
+ // records whether the thread should shut down or not.
+ private boolean c_stopThread = false;
+ // snooze between checks on the stop timer.
+ final long SNOOZE_PERIOD = 20;
+
+ /**
+ * creates a new single-shot ethread without starting it. this type of thread will run just once.
+ */
+ public ethread()
+ {
+ }
+
+ /**
+ * creates a new periodic ethread without starting it. this type of thread runs every "period" milliseconds until stopped or until the
+ * performActivity method returns false.
+ */
+ public ethread(long period)
+ {
+ c_period = period;
+ }
+
+ /**
+ * this is the main function that derived classes must implement. it does the actual work that the thread is intended to perform. note
+ * that the derived version must not do anything to cause the thread to be ripped out while performActivity is still being invoked. the
+ * return value should be true if the thread can continue executing. this is meaningless for single shot threads executed via runOnce, but
+ * matters for the periodic threads started with runPeriodic.
+ */
+ abstract public boolean performActivity();
+
+ /**
+ * Begins execution of the thread.
+ */
+ public void start()
+ {
+ synchronized (c_lock) {
+ if (null == this.c_RealThread) {
+ this.c_RealThread = new Thread(this);
+ c_logger.debug("starting thread " + c_RealThread.getId());
+ this.c_RealThread.start();
+ }
+ }
+ }
+
+ /**
+ * Stops execution of the thread, or at least attempts to.
+ */
+ public void stop()
+ {
+ cancel();
+ while (true) {
+ if (threadAlive()) {
+ try {
+ Thread.sleep(40);
+ } catch (InterruptedException e) {
+ // ignoring this since we'll keep snoozing as needed.
+ }
+ } else {
+ break;
+ }
+ }
+ }