KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > syntax > misc > ATEThread


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.ate.syntax.misc;
33
34 public abstract class ATEThread implements Runnable JavaDoc {
35
36     protected int threadSleep = 100;
37     protected int threshold = 0;
38     protected int default_threshold = 250;
39     protected boolean running = false;
40     protected boolean run = false;
41     protected Thread JavaDoc thread = null;
42     protected boolean asleep = false;
43     protected boolean skip = false;
44
45     public ATEThread() {
46     }
47
48     public void setDefaultThreshold(int threshold) {
49         this.default_threshold = threshold;
50     }
51
52     public synchronized void setRunning(boolean flag) {
53         running = flag;
54     }
55
56     public synchronized boolean isRunning() {
57         return running;
58     }
59
60     protected synchronized void start() {
61         this.threadSleep = Integer.MAX_VALUE;
62         start_();
63     }
64
65     protected synchronized void start(int threadSleep) {
66         this.threadSleep = threadSleep;
67         start_();
68     }
69
70     private void start_() {
71         if(!run) {
72             run = true;
73             thread = new Thread JavaDoc(this);
74             thread.setName(getClass().toString());
75             thread.start();
76         }
77     }
78
79     public synchronized void skip() {
80         skip = true;
81     }
82
83     private synchronized void resetSkip() {
84         skip = false;
85     }
86
87     public synchronized void stop() {
88         threshold = 0;
89         run = false;
90         thread.interrupt();
91     }
92
93     /** Interrupt the thread if it is sleeping. If threshold > 0, the thread will sleep an
94      * additionnal 'threshold' ms until no other awakeThread() is called
95      * before trying to compute something.
96      */

97     public synchronized void awakeThread() {
98         awakeThread(default_threshold);
99     }
100
101     public synchronized void awakeThread(int threshold) {
102         resetSkip();
103         this.threshold = threshold;
104         if(asleep)
105             thread.interrupt();
106     }
107
108     public boolean cancel() {
109         return !run;
110     }
111
112     protected abstract void threadRun() throws Exception JavaDoc;
113
114     public void threadReportException(Exception JavaDoc e) {
115         e.printStackTrace();
116     }
117
118     public boolean threadSleep(int ms) {
119         boolean interrupted = false;
120         asleep = true;
121         try {
122             Thread.sleep(ms);
123         } catch (InterruptedException JavaDoc e) {
124             interrupted = true;
125         }
126         asleep = false;
127         return interrupted;
128     }
129
130     public void run() {
131         setRunning(true);
132         while(run) {
133             if(threadSleep(threadSleep)) {
134                 // Sleep interrupted. If threshold is > 0, then wait
135
// this amount of time and loop until the thread is not
136
// anymore interrupted.
137
if(threshold>0) {
138                     while(threadSleep(threshold)) {
139                     }
140                 }
141             }
142
143             if(!run)
144                 break;
145
146             if(skip) {
147                 resetSkip();
148                 continue;
149             }
150
151             try {
152                 threadRun();
153             } catch(Exception JavaDoc e) {
154                 threadReportException(e);
155             }
156         }
157         setRunning(false);
158     }
159
160 }
161
Popular Tags