org.opengts.util
Class ThreadPool

java.lang.Object
  extended by org.opengts.util.ThreadPool

public class ThreadPool
extends java.lang.Object

Thread pool manager


Field Summary
static int STOP_NEVER
           
static int STOP_NOW
           
static int STOP_WAITING
           
 
Constructor Summary
ThreadPool(java.lang.String name)
          Constuctor
ThreadPool(java.lang.String name, int maxPoolSize)
          Constructor
ThreadPool(java.lang.String name, int maxPoolSize, int maxIdleSec, int maxQueueSize)
          Constructor
ThreadPool(java.lang.String name, RTKey propPfx_, int maxPoolSize, int maxIdleSec, int maxQueueSize)
          Constructor
 
Method Summary
protected  void _removeThreadJob(org.opengts.util.ThreadPool.ThreadJob thread)
          Removes the specified worker thread from the pool
 boolean equals(java.lang.Object other)
          Returns true if this object is equal to other.
 int getActiveCount()
          Gets the number of currently active jobs
 long getMaxIdleMS()
          Gets the maximum number of milliseconds that a thread is allowed to remain idle before it self terminates.
 int getMaxPoolSize()
          Gets the maximum size of this thread pool
 int getMaxQueueSize()
          Gets the maximum allowed number of waiting jobs (in "jobQueue")
 java.lang.String getName()
          Gets the name of the thread pool
 int getPoolSize()
          Gets the current size of this thread pool
 int getQueueSize()
          Gets the job queue size (jobs not yet processed)
 java.lang.ThreadGroup getThreadGroup()
          Gets the thread group of the Threads in this pool
static java.lang.StringBuffer GetThreadPoolState(java.lang.StringBuffer sb)
          Gets the ThreadPool state
static int GetTotalThreadCount()
          Tell all active threads to stop
static void main(java.lang.String[] argv)
          Main entry point for testing/debugging
static int PrintThreadCount()
          Tell all active threads to stop
 boolean run(java.lang.Runnable job)
          Adds a new job to the thread pool's queue
 void setMaxIdleMS(long maxIdleMS)
          Sets the maximum number of milliseconds that a thread is allowed to remain idle before it self terminates.
 void setMaxIdleSec(int maxIdleSec)
          Sets the maximum number of seconds that a thread is allowed to remain idle before it self-terminates.
 void setMaxIdleSec(RTKey propKeySec, int dftMaxIdleSec)
          Sets the maximum number of seconds that a thread is allowed to remain idle before it self-terminates.
 void setMaxPoolSize(int maxSize)
          Sets the maximum size of this thread pool
 void setMaxPoolSize(RTKey propKey, int dftMaxSize)
          Sets the maximum size of this thread pool
 void setMaxQueueSize(int maxQSize)
          Sets the maximum allowed number of waiting jobs (in "jobQueue")
 void setMaxQueueSize(RTKey propKey, int dftMaxQSize)
          Sets the maximum allowed number of waiting jobs (in "jobQueue")
 void stopThreads()
          Stops all threads in this pool once queued jobs are complete
 void stopThreads(boolean stopNow)
          Stops all threads in this pool once queued jobs are complete
static void StopThreads(boolean stopNow)
          Tell all active threads to stop
 java.lang.String toString()
          Returns the name of the thread pool
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STOP_WAITING

public static final int STOP_WAITING
See Also:
Constant Field Values

STOP_NEVER

public static final int STOP_NEVER
See Also:
Constant Field Values

STOP_NOW

public static final int STOP_NOW
See Also:
Constant Field Values
Constructor Detail

ThreadPool

public ThreadPool(java.lang.String name)
Constuctor

Parameters:
name - The name of the thread pool

ThreadPool

public ThreadPool(java.lang.String name,
                  int maxPoolSize)
Constructor

Parameters:
name - The name of the thread pool
maxPoolSize - The maximum number of threads in the thread pool

ThreadPool

public ThreadPool(java.lang.String name,
                  int maxPoolSize,
                  int maxIdleSec,
                  int maxQueueSize)
Constructor

Parameters:
name - The name of the thread pool
maxPoolSize - The maximum number of threads in the thread pool
maxIdleSec - The maximum number of seconds a thread is allowed to remain idle before it self-terminates.
maxQueueSize - The maximum number of jobs allowed in queue

ThreadPool

public ThreadPool(java.lang.String name,
                  RTKey propPfx_,
                  int maxPoolSize,
                  int maxIdleSec,
                  int maxQueueSize)
Constructor

Parameters:
name - The name of the thread pool
propPfx_ - The property key prefix from which the default attributes for this ThreadPool will be obtained.
maxPoolSize - The maximum number of threads in the thread pool ("maximumPoolSize")
maxIdleSec - The maximum number of seconds a thread is allowed to remain idle before it self-terminates ("maximumIdleSeconds")
maxQueueSize - The maximum number of jobs allowed in queue ("maximumQueueSize")
Method Detail

StopThreads

public static void StopThreads(boolean stopNow)
Tell all active threads to stop

Parameters:
stopNow - True to stop threads, even if jobs are still queued. False to stop only after all jobs have been processed. (note that jobs currently being processed will continue until they are done).

GetTotalThreadCount

public static int GetTotalThreadCount()
Tell all active threads to stop


PrintThreadCount

public static int PrintThreadCount()
Tell all active threads to stop


GetThreadPoolState

public static java.lang.StringBuffer GetThreadPoolState(java.lang.StringBuffer sb)
Gets the ThreadPool state


getName

public java.lang.String getName()
Gets the name of the thread pool

Returns:
The name of the thread pool

toString

public java.lang.String toString()
Returns the name of the thread pool

Overrides:
toString in class java.lang.Object
Returns:
The name of the thread pool

equals

public boolean equals(java.lang.Object other)
Returns true if this object is equal to other. This will only return true if they are the same object

Overrides:
equals in class java.lang.Object
Parameters:
other - The object to check equality with
Returns:
True if other is the same object

getThreadGroup

public java.lang.ThreadGroup getThreadGroup()
Gets the thread group of the Threads in this pool

Returns:
The thread group of the Threads in this pool

getActiveCount

public int getActiveCount()
Gets the number of currently active jobs


getPoolSize

public int getPoolSize()
Gets the current size of this thread pool

Returns:
The number of thread jobs in this thread pool

setMaxPoolSize

public void setMaxPoolSize(int maxSize)
Sets the maximum size of this thread pool

Parameters:
maxSize - The maximum size of the thread pool

setMaxPoolSize

public void setMaxPoolSize(RTKey propKey,
                           int dftMaxSize)
Sets the maximum size of this thread pool

Parameters:
propKey - The property key name to use for looking up the overriding value in the runtime configuration properties.
dftMaxSize - The maximum size of the thread pool

getMaxPoolSize

public int getMaxPoolSize()
Gets the maximum size of this thread pool

Returns:
The maximum size of the thread pool

setMaxIdleSec

public void setMaxIdleSec(int maxIdleSec)
Sets the maximum number of seconds that a thread is allowed to remain idle before it self-terminates.

Parameters:
maxIdleSec - The maximum number of idle seconds

setMaxIdleSec

public void setMaxIdleSec(RTKey propKeySec,
                          int dftMaxIdleSec)
Sets the maximum number of seconds that a thread is allowed to remain idle before it self-terminates.

Parameters:
propKeySec - The propery name from which the maximum number of idle seconds will attempt to be retrieved.
dftMaxIdleSec - The default maximum number of idle seconds.

setMaxIdleMS

public void setMaxIdleMS(long maxIdleMS)
Sets the maximum number of milliseconds that a thread is allowed to remain idle before it self terminates.

Parameters:
maxIdleMS - The maximum number of idle milliseconds

getMaxIdleMS

public long getMaxIdleMS()
Gets the maximum number of milliseconds that a thread is allowed to remain idle before it self terminates.

Returns:
The maximum idle milliseconds

setMaxQueueSize

public void setMaxQueueSize(int maxQSize)
Sets the maximum allowed number of waiting jobs (in "jobQueue")

Parameters:
maxQSize - The maximum allowed number of waiting jobs

setMaxQueueSize

public void setMaxQueueSize(RTKey propKey,
                            int dftMaxQSize)
Sets the maximum allowed number of waiting jobs (in "jobQueue")

Parameters:
propKey - The property key name to use for looking up the overriding value in the runtime configuration properties.
dftMaxQSize - The maximum allowed number of waiting jobs

getMaxQueueSize

public int getMaxQueueSize()
Gets the maximum allowed number of waiting jobs (in "jobQueue")

Returns:
The maximum allowed number of waiting jobs (in "jobQueue")

run

public boolean run(java.lang.Runnable job)
Adds a new job to the thread pool's queue

Parameters:
job - The job to add to the queue

getQueueSize

public int getQueueSize()
Gets the job queue size (jobs not yet processed)


stopThreads

public void stopThreads()
Stops all threads in this pool once queued jobs are complete


stopThreads

public void stopThreads(boolean stopNow)
Stops all threads in this pool once queued jobs are complete

Parameters:
stopNow - True to stop threads, even if jobs are still queued. False to stop only after all jobs have been processed. (note that jobs currently being processed will continue until they are done).

_removeThreadJob

protected void _removeThreadJob(org.opengts.util.ThreadPool.ThreadJob thread)
Removes the specified worker thread from the pool

Parameters:
thread - The thread to remove from the pool

main

public static void main(java.lang.String[] argv)
Main entry point for testing/debugging

Parameters:
argv - Comand-line arguments