net.sourceforge.groboutils.util.thread.v1
Class QueueThread

java.lang.Object
  extended bynet.sourceforge.groboutils.util.thread.v1.LoopThread
      extended bynet.sourceforge.groboutils.util.thread.v1.QueueThread

public class QueueThread
extends LoopThread

For threads which endlessly process events from a SynchQueue. This is a common technique for thread pooling.

Users must make a implementation of IObjectListener. If the user does not give a SynchQueue, it is created for them. Once the QueueThread is created, the queue and the listener cannot be changed.

By default, the underlying 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.

It is advisable not to use the methods LoopThread.setSleepTime( int ) or LoopThread.setSleepTimeMillis( long ), since these will cause the QueueThread 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.

Since the 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 LoopThread.stop() or LoopThread.suspend() signal. Thanks to Dominique Gallot for pointing this out.

After a LoopThread.stop() is invoked, you may allow the object listener to finish processing the remaining elements in the inner queue by calling processRemaining().

Since:
June 4, 2000
Version:
$Date: 2003/02/10 22:52:48 $
Author:
Matt Albrecht groboclown@users.sourceforge.net

Field Summary
 
Fields inherited from class net.sourceforge.groboutils.util.thread.v1.LoopThread
MILLI_IN_SECOND
 
Constructor Summary
QueueThread(IObjectListener ol)
           
QueueThread(IObjectListener ol, java.lang.String threadName)
           
QueueThread(IObjectListener ol, SynchQueue sq)
           
QueueThread(IObjectListener ol, SynchQueue sq, java.lang.String threadName)
           
QueueThread(IObjectListener ol, SynchQueue sq, java.lang.ThreadGroup tg)
           
QueueThread(IObjectListener ol, SynchQueue sq, java.lang.ThreadGroup tg, java.lang.String threadName)
           
QueueThread(IObjectListener ol, java.lang.ThreadGroup tg)
           
QueueThread(IObjectListener ol, java.lang.ThreadGroup tg, java.lang.String threadName)
           
 
Method Summary
 SynchQueue getQueue()
          Retrieves the internal listened queue.
 long getTimeoutMilliseconds()
          Retrieve the millisecond part of the maximum timeout to wait for an incoming element on the inner queue before checking for thread event signals.
 int getTimeoutNanoseconds()
          Retrieve the nanosecond part of the maximum timeout to wait for an incoming element on the inner queue before checking for thread event signals.
protected  void initialize(IObjectListener ol, SynchQueue sq)
           
protected  void initializeDefaults()
           
 boolean isProcessingObjects()
           
 void processRemaining()
          Process all elements in the queue until the queue is empty.
 void setTimeout(long timeout)
          Set the maximum time (in milliseconds) to wait for an incoming element on the inner queue before checking for LoopThread.stop() and LoopThread.suspend() signals.
 void setTimeout(long timeout, int nanos)
          Set the maximum time (in milliseconds and nanoseconds) to wait for an incoming element on the inner queue before checking for LoopThread.stop() and LoopThread.suspend() signals.
 
Methods inherited from class net.sourceforge.groboutils.util.thread.v1.LoopThread
getPriority, getSleepTime, getSleepTimeMillis, getThreadGroup, isAlive, isDaemon, isPaused, isRunning, join, resume, setDaemon, setPriority, setRunnable, setSleepTime, setSleepTimeMillis, start, stop, suspend, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueueThread

public QueueThread(IObjectListener ol)

QueueThread

public QueueThread(IObjectListener ol,
                   SynchQueue sq)

QueueThread

public QueueThread(IObjectListener ol,
                   java.lang.ThreadGroup tg)

QueueThread

public QueueThread(IObjectListener ol,
                   SynchQueue sq,
                   java.lang.ThreadGroup tg)

QueueThread

public QueueThread(IObjectListener ol,
                   java.lang.String threadName)

QueueThread

public QueueThread(IObjectListener ol,
                   SynchQueue sq,
                   java.lang.String threadName)

QueueThread

public QueueThread(IObjectListener ol,
                   java.lang.ThreadGroup tg,
                   java.lang.String threadName)

QueueThread

public QueueThread(IObjectListener ol,
                   SynchQueue sq,
                   java.lang.ThreadGroup tg,
                   java.lang.String threadName)
Method Detail

getQueue

public SynchQueue getQueue()
Retrieves the internal listened queue.


isProcessingObjects

public boolean isProcessingObjects()
Returns:
false if the thread is waiting for an object to be placed in the queue and be processed, otherwise true.

setTimeout

public void setTimeout(long timeout,
                       int nanos)
Set the maximum time (in milliseconds and nanoseconds) to wait for an incoming element on the inner queue before checking for LoopThread.stop() and LoopThread.suspend() signals.

Parameters:
timeout - the maximum time to wait in milliseconds.
nanos - additional time, in nanoseconds range 0-999999.
See Also:
SynchQueue.dequeue( long, int )

setTimeout

public void setTimeout(long timeout)
Set the maximum time (in milliseconds) to wait for an incoming element on the inner queue before checking for LoopThread.stop() and LoopThread.suspend() signals.

Parameters:
timeout - the maximum time to wait in milliseconds.
See Also:
SynchQueue.dequeue( long, int ), setTimeout( long, int )

getTimeoutMilliseconds

public long getTimeoutMilliseconds()
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 Also:
setTimeout( long, int )

getTimeoutNanoseconds

public int getTimeoutNanoseconds()
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 Also:
setTimeout( long, int )

processRemaining

public void processRemaining()
                      throws java.lang.InterruptedException
Process all elements in the queue until the queue is empty. This may only be called while the thread is not running.

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).

Throws:
java.lang.InterruptedException

initialize

protected void initialize(IObjectListener ol,
                          SynchQueue sq)

initializeDefaults

protected void initializeDefaults()


Copyright © 2001-2003 by The GroboUtils Project