com.cometway.util
Class ThreadPool

java.lang.Object
  extended by com.cometway.util.ThreadPool

public class ThreadPool
extends java.lang.Object

This class starts and stops a pool of PooledThreads.


Field Summary
protected  java.util.Vector allocatedThreads
           
protected  int max_threads
           
protected  int nextID
           
 int num_threads
           
protected  int priority
           
protected  Queue queue
           
protected  java.lang.Object queueChanged
           
protected  boolean stopped
           
protected  java.lang.String threadPoolName
           
 
Constructor Summary
ThreadPool(int max_threads)
          Starts a ThreadPool with the max thread limit to the specified number.
ThreadPool(int max_threads, int thread_priority)
          Starts a ThreadPool with the max thread limit to the specified number and a priority setting for the PooledThreads.
 
Method Summary
protected  void error(java.lang.String s, java.lang.Exception e)
           
 int freeThreads()
          This returns the number of allocated PooledThreads that are free
 boolean getThread(KMethod method)
          This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in.
 boolean getThread(KMethod method, java.lang.Object obj)
          This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in.
 boolean getThread(KMethod method, java.lang.Object obj, int timeout)
          This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in.
 boolean getThreadOrWait(KMethod method, int timeout)
           
 boolean getThreadOrWait(KMethod method, java.lang.Object obj, int timeout)
           
static void main(java.lang.String[] args)
           
protected  PooledThread makePooledThread()
           
protected  void releaseThread(PooledThread t)
           
protected  void returnThread(PooledThread t)
           
 void setName(java.lang.String name)
          This method sets the Name of this ThreadPool.
 void stop()
          This method stops the ThreadPool allows it to be garbage collected.
 int totalThreads()
          This returns the total number of allocated PooledThreads.
 int usedThreads()
          This returns the number of allocated PooledThreads that are active.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queue

protected Queue queue

allocatedThreads

protected java.util.Vector allocatedThreads

num_threads

public int num_threads

max_threads

protected int max_threads

threadPoolName

protected java.lang.String threadPoolName

nextID

protected int nextID

priority

protected int priority

queueChanged

protected java.lang.Object queueChanged

stopped

protected boolean stopped
Constructor Detail

ThreadPool

public ThreadPool(int max_threads)
Starts a ThreadPool with the max thread limit to the specified number.


ThreadPool

public ThreadPool(int max_threads,
                  int thread_priority)
Starts a ThreadPool with the max thread limit to the specified number and a priority setting for the PooledThreads.

Method Detail

stop

public void stop()
This method stops the ThreadPool allows it to be garbage collected. This method MUST be called in order for the pool and its threads to be freed.


setName

public void setName(java.lang.String name)
This method sets the Name of this ThreadPool. This name will be the prefix name for all the thread created by this threadPool.


getThread

public boolean getThread(KMethod method,
                         java.lang.Object obj,
                         int timeout)
This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in. A lock on the obj is first obtained before anything is done. The obj will wait until the KMethod is finished executing and it gets notified, or the specified timeout has expired.

Parameters:
method - This is the KMethod to execute on a PooledThread
obj - This is the obj used to wait for the PooledThread to finish.
timeout - This is the number of milliseconds to wait for the PooledThread to finish.
Returns:
Returns true if no exceptions were caught during the PooledThread creation and/or execution.

getThread

public boolean getThread(KMethod method,
                         java.lang.Object obj)
This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in. A lock on the obj is first obtained before anything is done. The obj will wait until the KMethod is finished executing and it gets notified.

Parameters:
method - This is the KMethod to execute on a PooledThread
obj - This is the obj used to wait for the PooledThread to finish.
Returns:
Returns true if no exceptions were caught during the PooledThread creation and/or execution.

getThread

public boolean getThread(KMethod method)
This method gets an unused PooledThread or creates one if the pool has not exceeded its limit and executes the KMethod passed in. This method does not block on an object, this method returns immediately.

Parameters:
method - This is the KMethod to execute on a PooledThread
Returns:
Returns true if no exceptions were caught during the PooledThread creation and/or execution.

getThreadOrWait

public boolean getThreadOrWait(KMethod method,
                               int timeout)

getThreadOrWait

public boolean getThreadOrWait(KMethod method,
                               java.lang.Object obj,
                               int timeout)

totalThreads

public int totalThreads()
This returns the total number of allocated PooledThreads.


usedThreads

public int usedThreads()
This returns the number of allocated PooledThreads that are active.


freeThreads

public int freeThreads()
This returns the number of allocated PooledThreads that are free


makePooledThread

protected PooledThread makePooledThread()
                                 throws java.lang.NullPointerException
Throws:
java.lang.NullPointerException

returnThread

protected void returnThread(PooledThread t)

releaseThread

protected void releaseThread(PooledThread t)

error

protected void error(java.lang.String s,
                     java.lang.Exception e)

main

public static void main(java.lang.String[] args)