jason.service.pool
Class ThreadPool

java.lang.Object
  |
  +--jason.service.pool.ThreadPool
All Implemented Interfaces:
ThreadPoolInt

public class ThreadPool
extends java.lang.Object
implements ThreadPoolInt

This class is an generic implementation of a thread pool, which takes the following input
a) Size of the pool to be constructed
b) Name of the class which implements Runnable (which has a visible default constructor)
and constructs a thread pool with active threads that are waiting for activation. once the threads have finished processing they come back and wait once again in the pool.

This thread pool engine can be locked i.e. if some internal operation is performed on the pool then it is preferable that the thread engine be locked. Locking ensures that no new threads are issued by the engine. However, the currently executing threads are allowed to continue till they come back to the passivePool.

Author - Sanjay "Ironluca" Debnath Ironluca@yahoo.com
Last Modified - 9/03/2001


Field Summary
protected  ActivePoolInt activePool
          The pool where the currently active threads which are servicing the clients are kept.
protected  int activePoolSize
          Current no of threads in the pool which are not available for servicing.
protected  PassivePoolInt passivePool
          The pool where the currently passive threads which are not servicing the clients are kept.
protected  int passivePoolSize
          Current no of threads in the pool which are available for servicing.
protected  boolean poolLocked
          This represents the status of the pool - unlocked or locked.
protected  ThreadCapsuleInt tempThreadCapsule
          Temporary storage for ThreadCapsule objects.
protected  java.lang.Class threadClass
          The class for which the pool is constructed, it must implement runnable and have a visible default constructor.
protected  int totalPoolSize
          Initial size of the pool as indicated by the user.
 
Constructor Summary
ThreadPool(java.lang.Class threadClass, int totalPoolSize)
          The only visible constructor for the class.
 
Method Summary
 int getActivePoolSize()
          Returns the value of the activePoolSize variable.
 int getPassivePoolSize()
          Returns the value of the passivePoolSize variable.
 ThreadCapsuleInt getThreadCapsule()
          This method returns a ThreadCapsule object with the client processing thread embedded in it.
 java.lang.Class getThreadClass()
          Returns the class for which the pool is being maintained.
 int getTotalPoolSize()
          Returns the value of totalPoolSize variable.
protected  void initializePools(java.lang.Class threadClass, int totalPoolSize)
          This method is called by the constructor as well as setThreadClass method to initialize or reinitialize all the pools (ActivePool as well as PassivePool).
 boolean isPoolLocked()
          Returns the current value of the poolLocked variable
 void setPoolLocked(boolean lockingState)
          Toggles the locked or unlocked state of the pool
 void setThreadClass(java.lang.Class threadClass, int totalPoolSize)
          Destroyes the current pools (ActivePool as well as PassivePool) and reinitializes the pool.
 void setTotalPoolSize(int totalPoolSize)
          This method sets the size of the total pool (active + passive) pool - if the size of the active pool is more than the size of the passive pool then ClientUnderServiceException is thrown otherwise the size of the passive pool is reduced to (totalPoolSize-newPoolSize).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

totalPoolSize

protected int totalPoolSize
Initial size of the pool as indicated by the user.


activePoolSize

protected int activePoolSize
Current no of threads in the pool which are not available for servicing.


passivePoolSize

protected int passivePoolSize
Current no of threads in the pool which are available for servicing.


threadClass

protected java.lang.Class threadClass
The class for which the pool is constructed, it must implement runnable and have a visible default constructor.


activePool

protected ActivePoolInt activePool
The pool where the currently active threads which are servicing the clients are kept.


passivePool

protected PassivePoolInt passivePool
The pool where the currently passive threads which are not servicing the clients are kept.


poolLocked

protected boolean poolLocked
This represents the status of the pool - unlocked or locked. If pool is locked then no more threads are issued by the thread pool, this feature shall become useful when some internal change to the pool is being performed - like changing the size of the pool etc.


tempThreadCapsule

protected ThreadCapsuleInt tempThreadCapsule
Temporary storage for ThreadCapsule objects.

Constructor Detail

ThreadPool

public ThreadPool(java.lang.Class threadClass,
                  int totalPoolSize)
           throws IllegalClassException,
                  java.lang.IllegalAccessException,
                  java.lang.InstantiationException
The only visible constructor for the class. It takes as it's argument the java.lang.Class of the java.lang.Thread implementation and the size of the pool to create. The Class passed as the argument should implement the java.lang.Runnable interface.

Throws:
IllegalClassException - This exception is generated if the class supplied for pooling does not implement java.lang.Runnable in any form (either explicitely or by extending Thread).
java.lang.IllegalAccessException
java.lang.InstantiationException
Method Detail

initializePools

protected void initializePools(java.lang.Class threadClass,
                               int totalPoolSize)
                        throws IllegalClassException,
                               java.lang.IllegalAccessException,
                               java.lang.InstantiationException
This method is called by the constructor as well as setThreadClass method to initialize or reinitialize all the pools (ActivePool as well as PassivePool).

IllegalClassException
java.lang.IllegalAccessException
java.lang.InstantiationException

getThreadClass

public java.lang.Class getThreadClass()
Returns the class for which the pool is being maintained. This is a convenience method.

Specified by:
getThreadClass in interface ThreadPoolInt
Returns:
java.lang.Class - The Class for which the thread pool is being maintained.

setThreadClass

public void setThreadClass(java.lang.Class threadClass,
                           int totalPoolSize)
                    throws IllegalClassException,
                           java.lang.IllegalAccessException,
                           java.lang.InstantiationException
Destroyes the current pools (ActivePool as well as PassivePool) and reinitializes the pool. This method should not be called if there are current clients being serviced.
The working is as follows:
a)The thread pool is first locked. In the locked state no threads are issued for any request.
b)The initializePools method is called with the requisit parameters which reinitializes the ActivePool as well as the PassivePool. Thus effectively destroying the earlier pools.
c)The thread pool is unlocked to receive request and issuing threads.
It might be posible to save the earlier objects to introduce optimization.

Specified by:
setThreadClass in interface ThreadPoolInt
Throws:
IllegalClassException - This exception is thrown if the Class supplied does not implement java.lang.Runnable either directly or indirectly by extending java.lang.Thread.
java.lang.IllegalAccessException
java.lang.InstantiationException

getTotalPoolSize

public int getTotalPoolSize()
Returns the value of totalPoolSize variable. Which in effect is the total size of the pool being maintained (active pool + passive pool).

Specified by:
getTotalPoolSize in interface ThreadPoolInt

setTotalPoolSize

public void setTotalPoolSize(int totalPoolSize)
                      throws ClientsUnderServiceException,
                             IllegalClassException,
                             java.lang.IllegalAccessException,
                             java.lang.InstantiationException
This method sets the size of the total pool (active + passive) pool - if the size of the active pool is more than the size of the passive pool then ClientUnderServiceException is thrown otherwise the size of the passive pool is reduced to (totalPoolSize-newPoolSize).

NOTE: In effect, the pools are not destroyed, but the required number of objects are either added to the pool or removed from the pool (PassivePool). In case, the target size of the pool is less than the current number of thread objects in the PassivePool, some thread objects are taken out of the ActivePool. However, it is not possible to determine which thread objects have been taken out. This method may have detremental effect on threads that are right now running. However, since a thread is not garbage collected while it is still active, the servicing of the client/request should proceed without interruption. After the thread finishes execution, it shall be garbage collected.

Specified by:
setTotalPoolSize in interface ThreadPoolInt
ClientsUnderServiceException
IllegalClassException
java.lang.IllegalAccessException
java.lang.InstantiationException

getActivePoolSize

public int getActivePoolSize()
Returns the value of the activePoolSize variable. In effect the total number of active threads at any instance.
NOTE: the size of this pool cannot be changed individually.

Specified by:
getActivePoolSize in interface ThreadPoolInt
Returns:
int - The current size of the activePool.

getPassivePoolSize

public int getPassivePoolSize()
Returns the value of the passivePoolSize variable. In effect the total number of waiting threads at any instance.
NOTE: the size of this pool cannot be changed individually.

Specified by:
getPassivePoolSize in interface ThreadPoolInt
Returns:
int - The current size of the passivePool.

isPoolLocked

public boolean isPoolLocked()
Returns the current value of the poolLocked variable

Specified by:
isPoolLocked in interface ThreadPoolInt
Returns:
boolean - "true" if the pool is currently locked. "false" if not.

setPoolLocked

public void setPoolLocked(boolean lockingState)
Toggles the locked or unlocked state of the pool

Specified by:
setPoolLocked in interface ThreadPoolInt

getThreadCapsule

public ThreadCapsuleInt getThreadCapsule()
This method returns a ThreadCapsule object with the client processing thread embedded in it. This object is responsible for controlling the core thread, if there are no passive threads in the pool this method returns null.

Specified by:
getThreadCapsule in interface ThreadPoolInt
Returns:
jason.service.pool.ThreadCapsule with the client servicing thread embedded in it.