1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* @(#)QueueThread.java 0.9.0 06/04/2000 - 13:31:52
*
* Copyright (C) 2000,,2003 2002 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.util.thread.v1;
import net.sourceforge.groboutils.util.datastruct.v1.SynchQueue;
/**
* For threads which endlessly process events from a SynchQueue. This
* is a common technique for thread pooling.
* <P>
* Users must make a implementation of <tt>IObjectListener</tt>.
* If the user does not give a <tt>SynchQueue</tt>, it is created for them.
* Once the QueueThread is created, the queue and the listener cannot
* be changed.
* <P>
* By default, the underlying {@link LoopThread} does not sleep between events,
* it is a daemon thread, runs on the lowest thread priority, and has
* not started yet. Of course, this can all be changed.
* <P>
* It is advisable not to use the methods {@link
* LoopThread#setSleepTime( int )} or
* {@link LoopThread#setSleepTimeMillis( long )}, since these will cause the
* <tt>QueueThread</tt> to not respond to incoming requests during this sleep
* time. However, there may be occations when this is necessary, so this is
* left available to the user.
* <P>
* Since the {@link SynchQueue#dequeue()} method can wait indefinitely, you may
* opt to set the dequeue's timeout to something other than 0. Without setting
* this, the thread may wait indefinitely for an incoming element to the queue
* without ever checking for a {@link LoopThread#stop()} or
* {@link LoopThread#suspend()} signal. Thanks to
* <a href="mailto:d.gallot@atoseuronext.be">Dominique Gallot</a> for pointing
* this out.
* <P>
* After a {@link LoopThread#stop()} is invoked, you may allow the
* object listener to finish processing the remaining elements in the
* inner queue by calling {@link #processRemaining()}.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @since June 4, 2000
* @version $Date: 2003/02/10 22:52:48 $
*/
public class QueueThread extends LoopThread
{
private IObjectListener m_objListener = null;
private SynchQueue m_queue = null;
private boolean m_isProcessingObject = false;
private long m_timeout = 0;
private int m_nanos = 0;
/**
* The runnable class - keep it private so no one
* can directly access it in a thread but us.
*/
private class QueueRunnable implements Runnable
{
public void run()
{
if (m_queue.isEmpty())
{
m_isProcessingObject = false;
}
try
{
Object o = m_queue.dequeue( m_timeout, m_nanos );
m_isProcessingObject = true;
m_objListener.processObject( o );
if (m_queue.isEmpty())
{
m_isProcessingObject = false;
}
}
catch (InterruptedException ie)
{
// whoops - how do we handle this?
}
}
}
//--------------------------------------------------------------
// Constructors
/**
*
*/
public QueueThread( IObjectListener ol )
{
this( ol, new SynchQueue() );
}
/**
*
*/
public QueueThread( IObjectListener ol, SynchQueue sq )
{
super();
initialize( ol, sq );
}
/**
*
*/
public QueueThread( IObjectListener ol, ThreadGroup tg )
{
this( ol, new SynchQueue(), tg );
}
/**
*
*/
public QueueThread( IObjectListener ol, SynchQueue sq, ThreadGroup tg )
{
super( null, tg );
initialize( ol, sq );
}
/**
*
*/
public QueueThread( IObjectListener ol, String threadName )
{
this( ol, new SynchQueue(), threadName );
}
/**
*
*/
public QueueThread( IObjectListener ol, SynchQueue sq, String threadName )
{
super( null, threadName );
initialize( ol, sq );
}
/**
*
*/
public QueueThread( IObjectListener ol, ThreadGroup tg, String threadName )
{
this( ol, new SynchQueue(), tg, threadName );
}
/**
*
*/
public QueueThread( IObjectListener ol, SynchQueue sq, ThreadGroup tg,
String threadName )
{
super( null, tg, threadName );
initialize( ol, sq );
}
//--------------------------------------------------------------
// Public Methods
/**
* Retrieves the internal listened queue.
*/
public SynchQueue getQueue()
{
return this.m_queue;
}
/**
* @return <tt>false</tt> if the thread is waiting for an object
* to be placed in the queue and be processed, otherwise
* <tt>true</tt>.
*/
public boolean isProcessingObjects()
{
return this.m_isProcessingObject;
}
/**
* Set the maximum time (in milliseconds and nanoseconds) to wait for
* an incoming element on the inner queue before checking for
* {@link LoopThread#stop()} and {@link LoopThread#suspend()}
* signals.
*
* @param timeout the maximum time to wait in milliseconds.
* @param nanos additional time, in nanoseconds range 0-999999.
* @see SynchQueue#dequeue( long, int )
*/
public void setTimeout( long timeout, int nanos )
{
this.m_timeout = timeout;
this.m_nanos = nanos;
}
/**
* Set the maximum time (in milliseconds) to wait for
* an incoming element on the inner queue before checking for
* {@link LoopThread#stop()} and {@link LoopThread#suspend()}
* signals.
*
* @param timeout the maximum time to wait in milliseconds.
* @param nanos additional time, in nanoseconds range 0-999999.
* @see SynchQueue#dequeue( long, int )
* @see #setTimeout( long, int )
*/
public void setTimeout( long timeout )
{
setTimeout( timeout, 0 );
}
/**
* Retrieve the millisecond part of the maximum timeout to wait for an
* incoming element on the inner queue before checking for thread
* event signals.
*
* @see #setTimeout( long, int )
*/
public long getTimeoutMilliseconds()
{
return this.m_timeout;
}
/**
* Retrieve the nanosecond part of the maximum timeout to wait for an
* incoming element on the inner queue before checking for thread
* event signals.
*
* @see #setTimeout( long, int )
*/
public int getTimeoutNanoseconds()
{
return this.m_nanos;
}
/**
* Process all elements in the queue until the queue is empty.
* This may only be called while the thread is not running.
* <P>
* This should be invoked with care, as it can cause an infinite
* loop if another thread is pushing in data after this processing
* thread has finished (but, that could also lead to an out-of-memory
* error if this method is never invoked).
*/
public void processRemaining()
throws InterruptedException
{
if (isRunning())
{
throw new IllegalStateException(
"cannot call processRemaining() while the underlying thread "+
"is still running." );
}
if (!this.m_queue.isEmpty())
{
this.m_isProcessingObject = true;
do
{
Object o = this.m_queue.dequeue();
this.m_objListener.processObject( o );
}
while (!this.m_queue.isEmpty());
this.m_isProcessingObject = false;
}
}
//--------------------------------------------------------------
// Protected Methods
protected void initialize( IObjectListener ol, SynchQueue sq )
{
setRunnable( new QueueRunnable() );
this.m_objListener = ol;
this.m_queue = sq;
initializeDefaults();
}
protected void initializeDefaults()
{
setSleepTime( 0 );
setDaemon( true );
setPriority( Thread.MIN_PRIORITY );
}
}
|