/Users/lyon/j4p/src/gui/run/RunJob.java

1    package gui.run; 
2     
3    public abstract class RunJob 
4            implements Runnable { 
5        /** 
6         * The minimum priority that a RunJob can 
7         * have. 
8         */ 
9        public static final int MIN_PRIORITY = 1; 
10       /** 
11        * The default priority that is assigned to a 
12        * RunJob. 
13        */ 
14       public static final int NORM_PRIORITY = 5; 
15       /** 
16        * The maximum priority that a RunJob can 
17        * have. 
18        */ 
19       public static final int MAX_PRIORITY = 10; 
20       private CommandThread t = 
21               new CommandThread(this); 
22       private long ms = 0; 
23    
24       private boolean cowsComeHome = true; 
25       private int count = Integer.MIN_VALUE; 
26       private int numberOfTimesRun = 0; 
27       private String name = "runJob"; 
28       private boolean isDaemon = false; 
29       private int priority = NORM_PRIORITY; 
30    
31    
32       /** 
33        * Runs the <code>doCommand()</command> every 
34        * <code>seconds</seconds>. The job starts 
35        * right away. 
36        *  
37        * @param seconds  
38        */ 
39       public RunJob(double seconds) { 
40           this(seconds, false); 
41       } 
42    
43       /** 
44        * Run the RunJob every <code> seconds</code> 
45        * If <code>wait</code> is <code>true</code> 
46        * then do not start the RunJob until the 
47        * start method is invoked. Otherwise, start 
48        * right away. 
49        *  
50        * @param seconds  
51        * @param wait     
52        */ 
53       public RunJob(double seconds, boolean wait) { 
54           this(seconds, wait, Integer.MIN_VALUE); 
55       } 
56    
57       /** 
58        * Run the RunJob every <code> seconds</code> 
59        * If <code>wait</code> is <code>true</code> 
60        * then do not start the RunJob until the 
61        * start method is invoked. Otherwise, start 
62        * right away. If the <code>count</count> is 
63        * present, then the job only executes 
64        * <code>count</count>. 
65        *  
66        * @param seconds  
67        * @param wait     
68        * @param count    
69        */ 
70       public RunJob(double seconds, 
71                     boolean wait, 
72                     int count) { 
73           this(seconds,wait,count,false); 
74       } 
75    
76       /** 
77        * Run the RunJob every <code> seconds</code> 
78        * If <code>wait</code> is <code>true</code> 
79        * then do not start the RunJob until the 
80        * start method is invoked. Otherwise, start 
81        * right away. If the <code>count</count> is 
82        * present, then the job only executes 
83        * <code>count</count>. <code>isDaemon</code> 
84        * Marks this RunJob as 
85        * either a daemon RunJob or a user RunJob. 
86        * The Java Virtual Machine exits when the 
87        * only RunJobs running are all daemon 
88        * RunJobs and all Threads are daemon threads. 
89        * The default value for <code>isDaemon</code> 
90        * is <code>false</code>. 
91        * 
92        * @param seconds seconds of wait between jobs 
93        * @param wait   true if you want to delay start 
94        * @param count  number of times to execute 
95        * @param isDaemon true if you want to exit on main exit 
96        */ 
97       public RunJob(double seconds, 
98                     boolean wait, 
99                     int count, 
100                    boolean isDaemon) { 
101          this.count = count; 
102          ms = (long) (seconds * 1000); 
103          if (wait == true) return; 
104          this.isDaemon = isDaemon; 
105          start(); 
106      } 
107   
108      /** 
109       * @return the number of times the RunJob was 
110       *         run. 
111       */ 
112      public int getNumberOfTimesRun() { 
113          return numberOfTimesRun; 
114      } 
115   
116   
117      /** 
118       * Changes the priority of this RunJob. 
119       * <p/> 
120       * This may result in throwing a 
121       * <code>SecurityException</code>. 
122       * <p/> 
123       * Otherwise, the priority of this RunJob is 
124       * set to the smaller of the specified 
125       * <code>newPriority</code> and the maximum 
126       * permitted priority of the RunJobs's thread 
127       * group. If the priority is smaller than 
128       * <code>MIN_PRIORITY</code> then it will be 
129       * set to <code>MIN_PRIORITY</code>. If the 
130       * priority is greater than <code>MAX_PRIORITY</code> 
131       * then it will be set to <code>MAX_PRIORITY</code>. 
132       * This avoids illegal arguments. 
133       * 
134       * @param priority priority to set this RunJob 
135       *                 to 
136       * @throws SecurityException if the current 
137       *                           RunJob cannot 
138       *                           modify its thread. 
139       * @see RunJob#getPriority 
140       * @see #MAX_PRIORITY 
141       * @see #MIN_PRIORITY 
142       */ 
143      public void setPriority(int priority) { 
144          if (priority > MAX_PRIORITY) 
145              priority = MAX_PRIORITY; 
146          if (priority < MIN_PRIORITY) 
147              priority = MIN_PRIORITY; 
148          this.priority = priority; 
149          t.setPriority(priority); 
150      } 
151   
152      /** 
153       * Returns this RunJob's priority. 
154       *  
155       * @return this RunJob's priority. 
156       * @see #setPriority 
157       * @see java.lang.Thread#setPriority(int) 
158       */ 
159      public final int getPriority() { 
160          return t.getPriority(); 
161      } 
162   
163      /** 
164       * Start will restart a RunJob that was 
165       * stopped. It will also start a RunJob that 
166       * was never started. 
167       */ 
168      public void start() { 
169          cowsComeHome = true; 
170          t = new CommandThread(this); 
171          t.setName(name); 
172          t.setDaemon(isDaemon()); 
173          t.setPriority(priority); 
174          t.start(); 
175      } 
176   
177      /** 
178       * Changes the name of this RunJob (and thread 
179       * upon which the RunJob is based, to be equal 
180       * to the argument <code>name</code>. 
181       * <p/> 
182       * First the <code>checkAccess</code> method 
183       * of this thread is called with no arguments. 
184       * This may result in throwing a 
185       * <code>SecurityException</code>. 
186       *  
187       * @param name the new name for this thread. 
188       */ 
189      public void setName(String name) { 
190          this.name = name; 
191          t.setName(name); 
192      } 
193   
194      /** 
195       * Gets the name of this RunJob (and the 
196       * thread upon which the RunJob is based 
197       * <p/> 
198       * First the <code>checkAccess</code> method 
199       * of this thread is called with no arguments. 
200       * This may result in throwing a 
201       * <code>SecurityException</code>. 
202       */ 
203      public String getName() { 
204          return t.getName(); 
205      } 
206   
207      /** 
208       * Stops the RunJob by causing a thread death. 
209       * A <code>start()</code> invocation Will make 
210       * a new thread and restart the RunJob. 
211       */ 
212      public void stop() { 
213          cowsComeHome = false; 
214      } 
215   
216      /** 
217       * Tests if the RunJob is a daemon RunJob. 
218       *  
219       * @return <code>true</code> if this RunJob is 
220       *         a daemon RunJob; <code>false</code> 
221       *         otherwise. 
222       */ 
223      public boolean isDaemon() { 
224          return isDaemon; 
225      }; 
226      
227   
228      public static void main(String args[]) { 
229          // anonymous inner class 
230          // That uses the command pattern 
231          // also uses facade pattern 
232          RunJob rj = new RunJob(2,false,5,false) { 
233              public void run() { 
234                  System.out.println( 
235                          new java.util.Date()); 
236              } 
237          }; 
238          rj.start(); 
239          rj.stop(); 
240          rj.start(); 
241      } 
242   
243      /** 
244       * Tests if this RunJob is alive. A RunJob is 
245       * alive if it has been started and has not 
246       * yet run for the requisite number of times. 
247       *  
248       * @return <code>true</code> if this RunJob is 
249       *         alive; <code>false</code> 
250       *         otherwise. 
251       */ 
252      public boolean isAlive() { 
253          return t.isCountDone() && cowsComeHome; 
254      } 
255   
256      /** 
257       * Returns a string representation of this 
258       * RunJob, including the RunJob's name, 
259       * priority, and thread group to which the 
260       * RunJob's thread belongs. 
261       *  
262       * @return a string representation of this 
263       *         thread. 
264       */ 
265      public String toString() { 
266          ThreadGroup group = t.getThreadGroup(); 
267          if (group != null) { 
268              return "RunJob[" + getName() + "," + 
269                     getPriority() + 
270                     "," + 
271                     group.getName() + "]"; 
272          } else { 
273              return "RunJob[" + getName() + "," + 
274                     getPriority() + 
275                     "," + 
276                     "" + "]"; 
277          } 
278      } 
279   
280   
281      private class CommandThread extends Thread { 
282          RunJob job = null; 
283   
284   
285          CommandThread(RunJob job) { 
286              this.job = job; 
287          } 
288   
289   
290          private boolean isCountDone() { 
291              if (count == Integer.MIN_VALUE) 
292                  return false; 
293              return numberOfTimesRun > count; 
294          } 
295   
296          public void run() { 
297              while (cowsComeHome) { 
298                  numberOfTimesRun++; 
299                  if (isCountDone()) return; 
300                  job.run(); 
301                  try { 
302                      Thread.sleep(ms); 
303                  } catch (InterruptedException e) { 
304                  } 
305              } 
306          } 
307      } 
308   
309  } 
310