net.groboclown.util.thread.v1
Class ThreadPool

java.lang.Object
  |
  +--net.groboclown.util.thread.v1.ThreadPool

public class ThreadPool
extends Object

A pool of QueueThread instances, each containing an instance of an ObjectListener implemented class. The Class to be the listener is passed into the constructor. Requirements for the Class are: 1. it implements QueueThread.ObjectListener, 2. it has a public constructor without any parameters.

The pool handles menial tasks such as:

  1. Growing the thread pool if the number of waiting objects is above a threshold number, up to a maximum number of threads.
  2. Finding the thread with the fewest number of waiting objects.
  3. Optimization of determining which thread to pass events to.

The pool gets much of its functionality by sharing a single SynchQueue between all of its threads.

Version:
Alpha 0.9.0
Author:
Matt Albrecht

Field Summary
private  int m_depthThreshold
           
private  int m_maxThreads
           
private  int m_numThreads
           
private  Class m_objListenerClass
           
private  Object m_objListenerInitData
           
private  QueueThread[] m_pool
           
private  SynchQueue m_sharedQueue
           
 
Constructor Summary
ThreadPool(Class objectListenerClass)
          Default constructor
ThreadPool(Class objectListenerClass, int maxThreads)
           
ThreadPool(Class objectListenerClass, Object initData)
           
ThreadPool(Class objectListenerClass, Object initData, int maxThreads)
           
ThreadPool(Class objectListenerClass, Object initData, int startingThreadCount, int maxThreads)
           
 
Method Summary
protected  QueueThread addNewThread()
          If there are not enough threads, then add one into the internal array, start the thread, and return the created thread.
 void addObject(Object o)
          Adds the given object into the shared queue, so that the next available thread will process it.
protected  void checkThreshold()
          Checks if the depth on the shared queue is too deep (beyond the threshold), and if so, creates a new thread to help deal with the situation.
protected  IObjectListener createObjectListenerInstance()
          Create an instance of the basic object listener class, as given in the constructor.
 int getMaximumThreadCount()
           
 int getObjectDepth()
           
 int getThreadCount()
           
 void resumeThreads()
          Resumes all threads.
 void setDepthThreshold(int threshold)
           
 void setMaximumThreadCount(int max)
           
 void stopThreads()
          Stops all threads.
 void suspendThreads()
          Suspends all threads.
 void waitForThreadsToFinish()
          Waits for all expecting objects in the queue to be processed, and for each thread to finish processing an object.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

m_objListenerClass

private Class m_objListenerClass

m_objListenerInitData

private Object m_objListenerInitData

m_pool

private QueueThread[] m_pool

m_sharedQueue

private SynchQueue m_sharedQueue

m_maxThreads

private int m_maxThreads

m_numThreads

private int m_numThreads

m_depthThreshold

private int m_depthThreshold
Constructor Detail

ThreadPool

public ThreadPool(Class objectListenerClass)
Default constructor

ThreadPool

public ThreadPool(Class objectListenerClass,
                  int maxThreads)

ThreadPool

public ThreadPool(Class objectListenerClass,
                  Object initData)
Parameters:
initData - if the given objectListenerClass is an instance of ThreadObjectListener, then the initData will be passed into the initialize( Object ) method.

ThreadPool

public ThreadPool(Class objectListenerClass,
                  Object initData,
                  int maxThreads)

ThreadPool

public ThreadPool(Class objectListenerClass,
                  Object initData,
                  int startingThreadCount,
                  int maxThreads)
Method Detail

setDepthThreshold

public void setDepthThreshold(int threshold)

getObjectDepth

public int getObjectDepth()

addObject

public void addObject(Object o)
Adds the given object into the shared queue, so that the next available thread will process it.

getThreadCount

public int getThreadCount()

getMaximumThreadCount

public int getMaximumThreadCount()

setMaximumThreadCount

public void setMaximumThreadCount(int max)

waitForThreadsToFinish

public void waitForThreadsToFinish()
Waits for all expecting objects in the queue to be processed, and for each thread to finish processing an object.

stopThreads

public void stopThreads()
Stops all threads.

suspendThreads

public void suspendThreads()
Suspends all threads.

resumeThreads

public void resumeThreads()
Resumes all threads.

addNewThread

protected QueueThread addNewThread()
If there are not enough threads, then add one into the internal array, start the thread, and return the created thread.
Returns:
the new thread, or null if the pool has exceeded its maximum thread count.

checkThreshold

protected void checkThreshold()
Checks if the depth on the shared queue is too deep (beyond the threshold), and if so, creates a new thread to help deal with the situation.

createObjectListenerInstance

protected IObjectListener createObjectListenerInstance()
Create an instance of the basic object listener class, as given in the constructor.
Throws:
IllegalStateException - thrown if there is an error creating a new instance of the class.


Written under the LGPL