improving this a little bit so it can be used someday.
[feisty_meow.git] / kona / src / org / feistymeow / process / ethread.java
1 package org.feistymeow.process;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 /**
7  * A simple java thread that hearkens back to the HOOPLE C++ ethread in features.
8  * 
9  * @author Chris Koeritz
10  * @copyright Copyright (c) 2010-$now By Feisty Meow Concerns Ltd.
11  * @license This file is free software; you can modify and redistribute it under the terms of the
12  *          Apache License v2.0: http://www.apache.org/licenses/LICENSE-2.0
13  */
14 public abstract class ethread implements Runnable
15 {
16         private static Log c_logger = LogFactory.getLog(ethread.class);
17
18         // the actual java thread object.
19         private volatile Thread c_RealThread = null;
20         // provides synchronization for the thread.
21         private volatile Object c_lock = new Object();
22
23         /**
24          * creates a new ethread without starting it.
25          */
26         public ethread()
27         {
28         }
29
30         /**
31          * Begins execution of the thread.
32          */
33         public void start()
34         {
35                 synchronized (c_lock) {
36                         if (null == this.c_RealThread) {
37                                 this.c_RealThread = new Thread(this);
38                                 c_logger.debug("starting thread " + c_RealThread.getId());
39                                 this.c_RealThread.start();
40                         }
41                 }
42         }
43
44         /**
45          * Stops execution of the thread, or at least attempts to.
46          */
47         public void stop()
48         {
49                 synchronized (c_lock) {
50                         Thread goAway = c_RealThread;
51                         c_RealThread = null;
52                         if (null != goAway) {
53                                 goAway.interrupt();
54                         }
55                 }
56         }
57
58         /**
59          * this is the main function that derived classes must implement. it does the actual work that
60          * the thread is intended to perform. note that the derived version must not do anything to
61          * cause the thread to be ripped out while performActivity is still being invoked.
62          */
63         // hmmm: should this stay void, or use a bool status to indicate if the thread should die?
64         abstract void performActivity();
65
66         /**
67          * Returns true if the thread isn't null.
68          */
69         public boolean threadRunning()
70         {
71                 synchronized (c_lock) {
72                         return (null != this.c_RealThread);
73                 }
74         }
75
76         /**
77          * this is the override from Runnable that allows us to call our own performActivity method.
78          * implementors should not override this; they should override performActivity instead.
79          */
80         public void run()
81         {
82                 synchronized (c_lock) {
83                         if (false == threadRunning()) {
84                                 return; // stopped before it ever started. how can this be? we just got invoked.
85                         }
86                         performActivity();
87                 }
88         }
89 }
90
91 // hmmm: still in progress...
92 // hmmm: missing the timed features of ethread.
93 // hmmm: missing cancel, exempt_stop, sleep_time, should_stop,
94