org.w3c.util
Class ThreadCache

java.lang.Object
  extended by org.w3c.util.ThreadCache

public class ThreadCache
extends java.lang.Object


Field Summary
protected  int cachesize
          Number of cached threads.
protected  org.w3c.util.CachedThread freelist
          List of free threads.
protected  org.w3c.util.CachedThread freetail
           
protected  java.lang.ThreadGroup group
          The thread group for this thread cache.
protected  boolean growasneeded
          Should we queue thread requests, rather then creating new threads.
protected  int idlethreads
          Number of idle threads to always maintain alive.
protected  int idletimeout
          The idle timeout, for a thread to wait before being killed.
protected  boolean inited
          Has this thread cache been initialized ?
protected  int threadcount
          Number of created threads.
protected  int threadid
          Uniq thread identifier within this ThreadCache instance.
protected  int threadpriority
          Cached thread priority.
protected  int usedthreads
          Number of used threads
 
Constructor Summary
ThreadCache(java.lang.String name)
          Create a thread cache, after creating a new thread group.
ThreadCache(java.lang.ThreadGroup group)
          Create a thread cache, whose threads are to be children of the group.
ThreadCache(java.lang.ThreadGroup parent, java.lang.String name)
          Create a thread cache, after creating a new thread group.
 
Method Summary
protected  org.w3c.util.CachedThread allocateThread(boolean waitp)
          Allocate a new thread, as requested.
 boolean getThread(java.lang.Runnable runnable, boolean waitp)
          Request a thread to run on the given object.
 java.lang.ThreadGroup getThreadGroup()
          Get the ThreadGroup managed by this ThreadCache instance.
 int getThreadPriority()
          Get the cached thread normal priority.
 void initialize()
          Initialize the given thread cache.
 void setCachesize(int cachesize)
          Set the thread cache size.
 void setCachesize(int cachesize, boolean update)
          Set the thread cache size.
 void setGrowAsNeeded(boolean onoff)
          Enable/disable the thread cache to grow as needed.
 void setIdleTimeout(int idletimeout)
          Set the idle timeout.
 void setThreadPriority(int priority)
          Set all the cached threads priority.
 void waitForCompletion()
          Wait until all the threads have finished their duty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inited

protected boolean inited
Has this thread cache been initialized ?


group

protected java.lang.ThreadGroup group
The thread group for this thread cache.


cachesize

protected int cachesize
Number of cached threads.


threadcount

protected int threadcount
Number of created threads.


threadid

protected int threadid
Uniq thread identifier within this ThreadCache instance.


idlethreads

protected int idlethreads
Number of idle threads to always maintain alive.


growasneeded

protected boolean growasneeded
Should we queue thread requests, rather then creating new threads.


usedthreads

protected int usedthreads
Number of used threads


freelist

protected org.w3c.util.CachedThread freelist
List of free threads.


freetail

protected org.w3c.util.CachedThread freetail

idletimeout

protected int idletimeout
The idle timeout, for a thread to wait before being killed. Defaults to 5000 milliseconds.


threadpriority

protected int threadpriority
Cached thread priority.

Constructor Detail

ThreadCache

public ThreadCache(java.lang.ThreadGroup group)
Create a thread cache, whose threads are to be children of the group.

Parameters:
group - The thread group to which this thread cache is bound.
nstart - Number of thread to create in advance.

ThreadCache

public ThreadCache(java.lang.String name)
Create a thread cache, after creating a new thread group.

Parameters:
name - The name of the thread group to create.

ThreadCache

public ThreadCache(java.lang.ThreadGroup parent,
                   java.lang.String name)
Create a thread cache, after creating a new thread group.

Parameters:
parent - The parent of the thread group to create.
name - The name of the thread group.
Method Detail

allocateThread

protected org.w3c.util.CachedThread allocateThread(boolean waitp)
Allocate a new thread, as requested.

Parameters:
waitp - Should we wait until a thread is available ?
Returns:
A launched CachedThread instance, or null if unable to allocate a new thread, and waitp is false.

setCachesize

public void setCachesize(int cachesize,
                         boolean update)
Set the thread cache size. This will also update the number of idle threads to maintain, if requested.

Parameters:
cachesize - The new thread cache size.
update - If true also update the number of threads to maintain idle.

setCachesize

public void setCachesize(int cachesize)
Set the thread cache size. Updaet the number of idle threads to keep alive.

Parameters:
cachesize - The new thread cache size.

setGrowAsNeeded

public void setGrowAsNeeded(boolean onoff)
Enable/disable the thread cache to grow as needed. This flag should be turned on only if always getting a thread as fast as possible is critical.

Parameters:
onoff - The toggle.

setThreadPriority

public void setThreadPriority(int priority)
Set all the cached threads priority. Changing the cached thread priority should be done before the thread cache is initialized, it will not affect already created threads.

Parameters:
priority - The new cachewd threads priority.

getThreadPriority

public int getThreadPriority()
Get the cached thread normal priority.

Returns:
Currently assigned cached thread priority.

setIdleTimeout

public void setIdleTimeout(int idletimeout)
Set the idle timeout. The idle timeout value is used to eliminate threads that have remain idle for too long (although the thread cache will ensure that a decent minimal number of threads stay around).

Parameters:
idletimeout - The new idle timeout.

getThread

public boolean getThread(java.lang.Runnable runnable,
                         boolean waitp)
Request a thread to run on the given object.

Parameters:
runnable - The object to run with the allocated thread.
waitp - If true wait until a free thread is available, otherwise, return false.
Returns:
A boolean, true if a thread was successfully allocated for the given object, false otherwise.

getThreadGroup

public java.lang.ThreadGroup getThreadGroup()
Get the ThreadGroup managed by this ThreadCache instance.

Returns:
A ThreadGroup instance.

waitForCompletion

public void waitForCompletion()
Wait until all the threads have finished their duty


initialize

public void initialize()
Initialize the given thread cache. This two stage initialize method is done so that configuration of the thread cache can be done before any thread get actually created.