1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.threadpool;
18
19
20 import org.apache.turbine.services.TurbineServices;
21
22
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25
26 /***
27 * A thread that is used to process Runnables. This thread will wait until it is
28 * notified by another thread that it needs processing. However it will only
29 * process if getRunnable != null.
30 *
31 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
32 * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>
33 * @version $Id: RunnableThread.java,v 1.7 2004/02/23 03:51:31 jford Exp $
34 */
35 public class RunnableThread extends Thread
36 {
37 /***
38 * Static initialization of the logger for this class
39 */
40 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RunnableThread.class.getName());
41
42 private boolean running = false;
43 private static int next_id = 0;
44
45 /***
46 * Increment a counter so that we can identify threads
47 * easily.
48 */
49 private static synchronized int getNextId() {
50 return ++next_id;
51 };
52 private int id = 0;
53
54 /***
55 The runnable that you want to process
56 */
57 private Runnable runnable = null;
58
59 public RunnableThread()
60 {
61 super();
62 this.setDaemon(true);
63 }
64
65 /***
66 * Creates a new Thread in the specified threadgroup
67 *
68 * @param tg the Threadgroup which will contain the new Thread
69 */
70 public RunnableThread( ThreadGroup tg) {
71 super( tg, "Provisory");
72 this.id = getNextId();
73 super.setName("RunnableThread:" + this.id );
74 this.setPriority( Thread.MIN_PRIORITY );
75 this.setDaemon(true);
76
77 }
78
79 /***
80 * Creates a new Thread in the specified threadgroup and assigns
81 * it an id.
82 *
83 * @param tg the Threadgroup which will contain the new Thread
84 * @param id an identifier for the new Thread
85 */
86 public RunnableThread( ThreadGroup tg,
87 int id ) {
88
89 super( tg, "RunnableThread:" + id );
90 this.setPriority( Thread.MIN_PRIORITY );
91 this.setDaemon(true);
92 this.id = id;
93 }
94
95 /***
96 * Processes the Runnable object assigned to it, whenever one
97 * is available
98 */
99 public void run() {
100
101
102
103
104 boolean poolrunning = true;
105 while ( poolrunning ) {
106
107
108
109
110
111
112 this.setRunning( false );
113
114 this.setRunnable( null );
115
116 synchronized( this ) {
117 if( this.getPriority() !=
118 JetspeedThreadPoolService.DEFAULT_THREAD_PRIORITY ) {
119
120 this.setPriority( JetspeedThreadPoolService.DEFAULT_THREAD_PRIORITY );
121 }
122
123
124
125
126
127
128
129
130
131
132
133 ( (JetspeedThreadPoolService)TurbineServices
134 .getInstance()
135 .getService( ThreadPoolService.SERVICE_NAME ) )
136 .release( this );
137
138
139
140
141 if ( this.getRunnable() == null ) {
142
143 try {
144 this.wait();
145 } catch (InterruptedException e) {
146
147
148
149 } catch ( Throwable t ) {
150 logger.error("Throwable", t);
151
152 }
153
154 }
155
156 }
157
158
159 if ( this.getRunnable() != null ) {
160 this.setRunning( true );
161
162 try {
163
164 this.getRunnable().run();
165
166 } catch ( Throwable t ) {
167 logger.error( "A problem occured while trying to run your thread", t );
168 }
169
170 }
171
172 }
173
174 }
175
176
177
178 /***
179 * Set the Runnable process to execute
180 *
181 * @param runnable the Object to execute
182 */
183 public void setRunnable( Runnable runnable ) {
184 this.runnable = runnable;
185 }
186
187 /***
188 * Get the Runnable process executing
189 *
190 * @return the Object executed by this thread
191 */
192 public Runnable getRunnable() {
193 return this.runnable;
194 }
195
196 /***
197 * Test whether the thread is currently executing a process
198 *
199 * @return the status of this thread. If true, the thread is currently
200 * executing a Runnable process, if false it's waiting for a new process
201 */
202 private boolean isRunning() {
203 return this.running;
204 }
205
206 /***
207 * Set the running status of this thread.
208 *
209 * @return the status of this thread
210 */
211 private void setRunning( boolean running ) {
212 this.running = running;
213 }
214
215 /***
216 * Get the numeric identifier of this thread
217 *
218 * @return the identifier of the thread
219 */
220 public int getId() {
221 return this.id;
222 }
223 }