KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > thread > v1 > LoopThread


1 /*
2  * @(#)LoopThread.java 0.9.0 06/02/2000 - 23:55:23
3  *
4  * Copyright (C) 2000,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.thread.v1;
28
29
30 /**
31  * For threads which loop endlessly (which is a common thing), this
32  * class implements the pause, start, and stop routines since JDK 1.2
33  * deprecated theirs.
34  * <p>
35  * This class delegates the Thread object, so that this class doesn't
36  * have to emulate all those thread methods. In this way, a LoopThread
37  * should act just like a Thread.
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @since June 4, 2000
41  * @version $Date: 2003/02/10 22:52:48 $
42  */

43 public class LoopThread
44 {
45     public static final long MILLI_IN_SECOND = 1000;
46
47
48     /* thread state: request a pause */
49     private boolean tryPause = false;
50     
51     /* thread state: request a stop */
52     private boolean tryStop = false;
53     
54     /* thread state: is it paused? */
55     private boolean isPaused = false;
56     
57     /* thread state: is it running? */
58     private boolean isRunning = false;
59     
60     /* wait/notify object for paused thread */
61     private Object JavaDoc pauseObject = new Object JavaDoc();
62     
63     /* delay time to sleep */
64     private long sleepTime = 6 * MILLI_IN_SECOND;
65     
66     /* Thread which runs in a loop */
67     private Thread JavaDoc thisThread;
68     
69     /* the runnable to be executed at each loop */
70     private Runnable JavaDoc runnable;
71
72     /**
73      * The runnable class - keep it private so no one
74      * can directly access it in a thread but us.
75      */

76     private class LoopRunnable implements Runnable JavaDoc
77     {
78         public void run()
79         {
80             isRunning = true;
81             isPaused = false;
82             do
83             {
84                 // this slows things down, but we're
85
// a low priority anyway, so it doesn't really matter,
86
// besides, it makes the check more than safe.
87
synchronized( pauseObject )
88                 {
89                     if (tryPause)
90                     {
91                         isPaused = true;
92                         try
93                         {
94                             pauseObject.wait();
95                         }
96                         catch (InterruptedException JavaDoc ie)
97                         {
98                             // stop execution
99
tryStop = true;
100                         }
101                         isPaused = false;
102                         // after pausing, we should check if we've been
103
// stopped.
104
continue;
105                     }
106                 }
107                 // sleep
108

109                 if (sleepTime > 0 && !tryStop)
110                 {
111 //System.out.println("[Sleeping for "+sleepTime+" milliseconds]");
112
try { Thread.sleep( sleepTime ); }
113                     catch (InterruptedException JavaDoc ie) {}
114                 }
115     
116                 // Run the runnable
117
if (!tryStop)
118                 {
119 //System.out.println("[Running loop]");
120
runnable.run();
121                 }
122             } while (!tryStop);
123             isRunning = false;
124         }
125     }
126     
127     
128     /**
129      * Used in case the implementing class needs to postpone initialization
130      * until after the LoopThread construction.
131      */

132     public LoopThread()
133     {
134         this.thisThread = new Thread JavaDoc( new LoopRunnable() );
135     }
136     
137     /**
138      *
139      */

140     public LoopThread( Runnable JavaDoc loop )
141     {
142         this.runnable = loop;
143         this.thisThread = new Thread JavaDoc( new LoopRunnable() );
144     }
145     
146     /**
147      *
148      */

149     public LoopThread( Runnable JavaDoc loop, String JavaDoc threadName )
150     {
151         this.runnable = loop;
152         this.thisThread = new Thread JavaDoc( new LoopRunnable() );
153     }
154     
155     /**
156      *
157      */

158     public LoopThread( Runnable JavaDoc loop, ThreadGroup JavaDoc group )
159     {
160         this.runnable = loop;
161         this.thisThread = new Thread JavaDoc( group, new LoopRunnable() );
162     }
163     
164     /**
165      *
166      */

167     public LoopThread( Runnable JavaDoc loop, ThreadGroup JavaDoc group, String JavaDoc threadName )
168     {
169         this.runnable = loop;
170         this.thisThread = new Thread JavaDoc( group, new LoopRunnable(), threadName );
171     }
172
173     
174     /**
175      * @see java.lang.Thread#getPriority()
176      */

177     public int getPriority()
178     {
179         return this.thisThread.getPriority();
180     }
181
182         
183     /**
184      *
185      * @see java.lang.Thread#setPriority( int )
186      */

187     public void setPriority( int priority )
188     {
189         this.thisThread.setPriority( priority );
190     }
191
192     
193     /**
194      *
195      * @see java.lang.Thread#getThreadGroup()
196      */

197     public ThreadGroup JavaDoc getThreadGroup()
198     {
199         return this.thisThread.getThreadGroup();
200     }
201     
202     /**
203      *
204      * @see java.lang.Thread#isAlive()
205      */

206     public boolean isAlive()
207     {
208         return this.thisThread.isAlive();
209     }
210
211     
212     /**
213      *
214      * @see java.lang.Thread#isDaemon()
215      */

216     public boolean isDaemon()
217     {
218         return this.thisThread.isDaemon();
219     }
220     
221     
222     /**
223      *
224      * @see java.lang.Thread#setDaemon( boolean )
225      */

226     public void setDaemon( boolean on )
227     {
228         this.thisThread.setDaemon( on );
229     }
230     
231     /**
232      *
233      * @see java.lang.Thread#join()
234      */

235     public void join() throws InterruptedException JavaDoc
236     {
237         if (this.isRunning)
238         {
239             throw new InterruptedException JavaDoc("can't join - thread is running");
240         }
241         this.thisThread.join();
242     }
243     
244     
245     /**
246      * Sets the runnable instance after construction. It cannot be
247      * changed once the thread is running.
248      */

249     public void setRunnable( Runnable JavaDoc run )
250     {
251         if (this.isRunning)
252         {
253             throw new IllegalStateException JavaDoc("can't change a running thread's "+
254                 "runnable" );
255         }
256         this.runnable = run;
257     }
258     
259
260     
261     /**
262      *
263      * @see java.lang.Thread#start()
264      */

265     public void start()
266     {
267         if (this.isRunning)
268         {
269             throw new IllegalStateException JavaDoc("can't start a running thread");
270         }
271         this.tryPause = false;
272         this.tryStop = false;
273         this.thisThread.start();
274     }
275
276
277     /**
278      * A non-deprecated, nice-to-the-system, thread suspension method.
279      *
280      * @see java.lang.Thread#suspend()
281      */

282     public void suspend()
283     {
284         if (this.tryStop || !this.isRunning)
285         {
286             throw new IllegalStateException JavaDoc("Cannot pause a stopped thread");
287         }
288         this.tryPause = true;
289     }
290
291     /**
292      *
293      * @see java.lang.Thread#resume()
294      */

295     public void resume()
296     {
297         synchronized( this.pauseObject )
298         {
299             if (!this.isPaused) return;
300             this.tryPause = false;
301             this.pauseObject.notifyAll();
302         }
303     }
304     
305     /**
306      *
307      * @see java.lang.Thread#stop()
308      */

309     public void stop()
310     {
311         if (!this.isRunning)
312         {
313             return; // tried stopping a stopped thread!
314
}
315         this.tryStop = true;
316         
317         // In case the thread is paused, we should resume it to have it
318
// quit naturally.
319
resume();
320     }
321
322     /**
323      *
324      */

325     public boolean isPaused()
326     {
327         return this.isPaused;
328     }
329     
330     /**
331      *
332      */

333     public boolean isRunning()
334     {
335         return this.isRunning;
336     }
337     
338     /**
339      * Retrieves the sleep time in seconds.
340      */

341     public int getSleepTime()
342     {
343         return (int)(this.sleepTime / MILLI_IN_SECOND);
344     }
345     
346     /**
347      * Sets the sleep time in seconds.
348      */

349     public void setSleepTime( int seconds )
350     {
351         this.sleepTime = seconds * MILLI_IN_SECOND;
352     }
353     
354     /**
355      * Sets the sleep time in milliseconds.
356      */

357     public void setSleepTimeMillis( long millis )
358     {
359         this.sleepTime = millis;
360     }
361     
362     
363     /**
364      * Retrieves the sleep time in milliseconds.
365      */

366     public long getSleepTimeMillis()
367     {
368         return this.sleepTime;
369     }
370      
371      
372     
373     /**
374      *
375      * @see java.lang.Thread#toString()
376      */

377     public String JavaDoc toString()
378     {
379         return this.thisThread.toString();
380     }
381 }
382
Popular Tags