jason.service.pool
Class ThreadCapsule

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--jason.service.pool.ThreadCapsule
All Implemented Interfaces:
java.lang.Runnable, ThreadCapsuleInt

public class ThreadCapsule
extends java.lang.Thread
implements ThreadCapsuleInt

This class represents a layer or a capsule over the client processing thread and is responsible for managing the life-cycle of individual threads. Any interaction with the thread shall have to pass through the capsule.
Author - Sanjay"Ironluca"Debnath Ironluca@yahoo.com
Last Modified - 9/03/2001


Field Summary
protected  ActivePoolInt activePool
          Reference of the ActivePool for the capsule.
protected  java.lang.Runnable coreThread
          The thread which is embedded in this capsule and is responsible to process a client.
protected  boolean keepAlive
          This variable is used to control the thread embedded in the threadCapsule & to keep it alive or terminate it.
protected  PassivePoolInt passivePool
           
protected  boolean state
          The state of the capsule, active or passive - false indicates the thread is not servicing any client right now.
protected  java.lang.Class threadClass
          The class of the coreThread that is embeded in this threadCapsule.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ThreadCapsule(java.lang.Class threadClass)
          The only visible constructor for the class.
 
Method Summary
 void activateThreadCapsule()
          This method must be called by the client to activate the thread capsule as well as the embedded thread.
 ActivePoolInt getActivePool()
          This method returns the reference to the activePool of which th ThreadCapsule is a member.
 java.lang.Runnable getCoreThread()
          This method returns the coreThread stored in this ThreadCapsule.
 boolean getKeepAlive()
          Returns the value of the keepAlive variable of the ThreadCapsule.
 PassivePoolInt getPassivePool()
          This method returns the reference to the passivePool of which th ThreadCapsule is a member.
 boolean getState()
          This method merely returns the state of the ThreadCapsule.
 java.lang.Class getThreadClass()
          Returns the threadClass reference of the ThreadCapsule.
 void run()
          This overridden method is responsible to run the algorithm required to service the client.
 void setActivePool(ActivePoolInt activePool)
          This method sets the activePool reference of the ThreadCapsule.
 void setCoreThread(java.lang.Runnable coreThread)
          This method is provided for optimization purpose only and should only be called by the ThreadPool only.
 void setKeepAlive(boolean keepAlive)
          This method sets the keepAlive variable of the ThreadCapsule, which is responsible to control the life-cycle of the ThreadCapsule.
 void setPassivePool(PassivePoolInt passivePool)
          This method sets the passivePool reference of the ThreadCapsule.
 void setState(boolean state)
          This method sets the state variable of the ThreadCapsule.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

threadClass

protected java.lang.Class threadClass
The class of the coreThread that is embeded in this threadCapsule.
This class must implement java.lang.Runnable either explicitely or implicitly by extending java.lang.Thread.
This class must also have a public visible default constructor


coreThread

protected java.lang.Runnable coreThread
The thread which is embedded in this capsule and is responsible to process a client.


state

protected boolean state
The state of the capsule, active or passive - false indicates the thread is not servicing any client right now. true indicates that it is servicing some client.


keepAlive

protected boolean keepAlive
This variable is used to control the thread embedded in the threadCapsule & to keep it alive or terminate it. true - keep alive, false - marked for termination.


activePool

protected ActivePoolInt activePool
Reference of the ActivePool for the capsule.


passivePool

protected PassivePoolInt passivePool
Constructor Detail

ThreadCapsule

public ThreadCapsule(java.lang.Class threadClass)
              throws java.lang.InstantiationException,
                     java.lang.IllegalAccessException
The only visible constructor for the class. It is a critical part of the class and does some important initializations to the threadCapsule. This constructor is used by ThreadPool to create individual ThreadCapsules in the pool.

Throws:
java.lang.InstantiationException - If there is no visible default constructor for the threadClass.
java.lang.IllegalAccessException - If there is no public constructor for the threadClass.
Method Detail

run

public void run()
This overridden method is responsible to run the algorithm required to service the client.
In effect what it does is call the run() method on the coreThread.
The run() method of the coreThread is then responsible to do whatever is required to service the client.
Apart from the above - it also calls wait() and notify() method on itself to suspend and resume execution.
When a cycle of execution is over (the coreThread run() method returns), it takes itself off the activePool and pushes back to the passivePool.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setActivePool

public void setActivePool(ActivePoolInt activePool)
This method sets the activePool reference of the ThreadCapsule.

Specified by:
setActivePool in interface ThreadCapsuleInt
See Also:
ActivePool, ActivePoolInt

getActivePool

public ActivePoolInt getActivePool()
This method returns the reference to the activePool of which th ThreadCapsule is a member.

Specified by:
getActivePool in interface ThreadCapsuleInt
Returns:
jason.service.pool.ActivePoolInt - The reference to the ActivePool.
See Also:
ActivePool, ActivePoolInt

setPassivePool

public void setPassivePool(PassivePoolInt passivePool)
This method sets the passivePool reference of the ThreadCapsule.

Specified by:
setPassivePool in interface ThreadCapsuleInt
See Also:
PassivePool, PassivePoolInt

getPassivePool

public PassivePoolInt getPassivePool()
This method returns the reference to the passivePool of which th ThreadCapsule is a member.

Specified by:
getPassivePool in interface ThreadCapsuleInt
Returns:
jason.service.pool.PassivePoolInt - The reference to the PassivePool.
See Also:
PassivePool, PassivePoolInt

setState

public void setState(boolean state)
This method sets the state variable of the ThreadCapsule.
This method should only be called by the ThreadPool or the controller of the thread pool.
This method should never be called by any application apart from the ones mentioned above.

Specified by:
setState in interface ThreadCapsuleInt

getState

public boolean getState()
This method merely returns the state of the ThreadCapsule.

Specified by:
getState in interface ThreadCapsuleInt
Returns:
boolean - the state of the ThreadCapsule.

setKeepAlive

public void setKeepAlive(boolean keepAlive)
This method sets the keepAlive variable of the ThreadCapsule, which is responsible to control the life-cycle of the ThreadCapsule.
This method should only be called by the ThreadPool or the controller of the thread pool.

Specified by:
setKeepAlive in interface ThreadCapsuleInt

getKeepAlive

public boolean getKeepAlive()
Returns the value of the keepAlive variable of the ThreadCapsule.

Specified by:
getKeepAlive in interface ThreadCapsuleInt
Returns:
boolean - Indicating the keepAlive status of the ThreadCapsule. It is useful to check for dead ThreadCapsules.

getThreadClass

public java.lang.Class getThreadClass()
Returns the threadClass reference of the ThreadCapsule.

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

getCoreThread

public java.lang.Runnable getCoreThread()
This method returns the coreThread stored in this ThreadCapsule. The coreThread is responsible to service the client.

Specified by:
getCoreThread in interface ThreadCapsuleInt
Returns:
java.lang.Runnable - The thread embedded in the ThreadCapsule.

setCoreThread

public void setCoreThread(java.lang.Runnable coreThread)
This method is provided for optimization purpose only and should only be called by the ThreadPool only.

Parameters:
coreThread - - The thread to be embedded in the ThreadCapsule.

activateThreadCapsule

public void activateThreadCapsule()
This method must be called by the client to activate the thread capsule as well as the embedded thread. The generic run() method of the embedded thread should not be called as this may result in destabilization of the entitre pool.

Specified by:
activateThreadCapsule in interface ThreadCapsuleInt