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

java.lang.Object
  extended bynet.sourceforge.groboutils.util.thread.v1.ThreadPool

public class ThreadPool
extends java.lang.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.

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

Constructor Summary
ThreadPool(java.lang.Class objectListenerClass)
          Default constructor
ThreadPool(java.lang.Class objectListenerClass, int maxThreads)
           
ThreadPool(java.lang.Class objectListenerClass, java.lang.Object initData)
           
ThreadPool(java.lang.Class objectListenerClass, java.lang.Object initData, int maxThreads)
           
ThreadPool(java.lang.Class objectListenerClass, java.lang.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(java.lang.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, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(java.lang.Class objectListenerClass)
Default constructor


ThreadPool

public ThreadPool(java.lang.Class objectListenerClass,
                  int maxThreads)

ThreadPool

public ThreadPool(java.lang.Class objectListenerClass,
                  java.lang.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(java.lang.Class objectListenerClass,
                  java.lang.Object initData,
                  int maxThreads)

ThreadPool

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

setDepthThreshold

public void setDepthThreshold(int threshold)

getObjectDepth

public int getObjectDepth()

addObject

public void addObject(java.lang.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:
java.lang.IllegalStateException - thrown if there is an error creating a new instance of the class.


Copyright © 2001-2003 by The GroboUtils Project