org.apache.tomcat.util.threads

Class ThreadPool


public class ThreadPool
extends java.lang.Object

A thread pool that is trying to copy the apache process management. Should we remove this in favor of Doug Lea's thread package?

Authors:
Gal Shachor
Yoav Shapira

Nested Class Summary

static class
ThreadPool.ControlRunnable
A Thread object that executes various actions ( ThreadPoolRunnable ) under control of ThreadPool
static class
ThreadPool.MonitorRunnable
Periodically execute an action - cleanup in this case
static interface
ThreadPool.ThreadPoolListener
Interface to allow applications to be notified when a threads are created and stopped.

Field Summary

static int
MAX_SPARE_THREADS
static int
MAX_THREADS
static int
MAX_THREADS_MIN
static int
MIN_SPARE_THREADS
static int
WORK_WAIT_TIMEOUT
protected int
currentThreadCount
protected int
currentThreadsBusy
protected boolean
isDaemon
protected Vector
listeners
protected int
maxSpareThreads
protected int
maxThreads
protected int
minSpareThreads
protected ThreadPool.MonitorRunnable
monitor
protected String
name
Name of the threadpool
protected ThreadPool.ControlRunnable[]
pool
protected int
sequence
Sequence.
protected boolean
stopThePool
protected int
threadPriority
Thread priority.
protected Hashtable
threads
The threads that are part of the pool.

Constructor Summary

ThreadPool()
Constructor.

Method Summary

void
addThread(Thread t, ThreadPool.ControlRunnable cr)
void
addThreadPoolListener(ThreadPool.ThreadPoolListener tpl)
protected void
adjustLimits()
protected void
checkSpareControllers()
Called by the monitor thread to harvest idle threads.
static ThreadPool
createThreadPool(boolean jmx)
Create a ThreadPool instance.
int
getCurrentThreadCount()
int
getCurrentThreadsBusy()
boolean
getDaemon()
static int
getDebug()
int
getMaxSpareThreads()
int
getMaxThreads()
int
getMinSpareThreads()
ThreadPool.MonitorRunnable
getMonitor()
String
getName()
int
getSequence()
String[]
getThreadParam()
Return an array with the current "param" ( XXX better name ? ) of each thread.
int
getThreadPriority()
Returns the priority level of current and future threads in this pool.
String[]
getThreadStatus()
Return an array with the status of each thread.
Enumeration
getThreads()
boolean
isDaemon()
protected void
notifyThreadEnd(ThreadPool.ControlRunnable c)
Inform the pool that the specific thread finish.
protected void
openThreads(int toOpen)
Create missing threads.
void
removeThread(Thread t)
protected void
returnController(ThreadPool.ControlRunnable c)
Returns the thread to the pool.
void
run(Runnable r)
void
runIt(ThreadPoolRunnable r)
Executes a given Runnable on a thread in the pool, block if needed.
void
setDaemon(boolean b)
The default is true - the created threads will be in daemon mode.
void
setMaxSpareThreads(int maxSpareThreads)
void
setMaxThreads(int maxThreads)
void
setMinSpareThreads(int minSpareThreads)
void
setName(String name)
void
setThreadPriority(int threadPriority)
Sets the thread priority for current and future threads in this pool.
void
shutdown()
Stop the thread pool
void
start()
String
threadStatusString()
Debug display of the stage of each thread.

Field Details

MAX_SPARE_THREADS

public static final int MAX_SPARE_THREADS

Field Value:
50


MAX_THREADS

public static final int MAX_THREADS

Field Value:
200


MAX_THREADS_MIN

public static final int MAX_THREADS_MIN

Field Value:
10


MIN_SPARE_THREADS

public static final int MIN_SPARE_THREADS

Field Value:
4


WORK_WAIT_TIMEOUT

public static final int WORK_WAIT_TIMEOUT

Field Value:
60000


currentThreadCount

protected int currentThreadCount


currentThreadsBusy

protected int currentThreadsBusy


isDaemon

protected boolean isDaemon


listeners

protected Vector listeners


maxSpareThreads

protected int maxSpareThreads


maxThreads

protected int maxThreads


minSpareThreads

protected int minSpareThreads


monitor

protected ThreadPool.MonitorRunnable monitor


name

protected String name
Name of the threadpool


pool

protected ThreadPool.ControlRunnable[] pool


sequence

protected int sequence
Sequence.


stopThePool

protected boolean stopThePool


threadPriority

protected int threadPriority
Thread priority.


threads

protected Hashtable threads
The threads that are part of the pool. Key is Thread, value is the ControlRunnable

Constructor Details

ThreadPool

public ThreadPool()
Constructor.

Method Details

addThread

public void addThread(Thread t,
                      ThreadPool.ControlRunnable cr)


addThreadPoolListener

public void addThreadPoolListener(ThreadPool.ThreadPoolListener tpl)


adjustLimits

protected void adjustLimits()


checkSpareControllers

protected void checkSpareControllers()
Called by the monitor thread to harvest idle threads.


createThreadPool

public static ThreadPool createThreadPool(boolean jmx)
Create a ThreadPool instance.

Parameters:
jmx - UNUSED

Returns:
ThreadPool instance. If JMX support is requested, you need to call register() in order to set a name.


getCurrentThreadCount

public int getCurrentThreadCount()


getCurrentThreadsBusy

public int getCurrentThreadsBusy()


getDaemon

public boolean getDaemon()


getDebug

public static int getDebug()


getMaxSpareThreads

public int getMaxSpareThreads()


getMaxThreads

public int getMaxThreads()


getMinSpareThreads

public int getMinSpareThreads()


getMonitor

public ThreadPool.MonitorRunnable getMonitor()


getName

public String getName()


getSequence

public int getSequence()


getThreadParam

public String[] getThreadParam()
Return an array with the current "param" ( XXX better name ? ) of each thread. This is typically the last request.

Returns:


getThreadPriority

public int getThreadPriority()
Returns the priority level of current and future threads in this pool.

Returns:
The priority


getThreadStatus

public String[] getThreadStatus()
Return an array with the status of each thread. The status indicates the current request processing stage ( for tomcat ) or whatever the thread is doing ( if the application using TP provide this info )

Returns:


getThreads

public Enumeration getThreads()


isDaemon

public boolean isDaemon()


notifyThreadEnd

protected void notifyThreadEnd(ThreadPool.ControlRunnable c)
Inform the pool that the specific thread finish. Called by the ControlRunnable.run() when the runnable throws an exception.


openThreads

protected void openThreads(int toOpen)
Create missing threads.

Parameters:
toOpen - Total number of threads we'll have open


removeThread

public void removeThread(Thread t)


returnController

protected void returnController(ThreadPool.ControlRunnable c)
Returns the thread to the pool. Called by threads as they are becoming idel.


run

public void run(Runnable r)


runIt

public void runIt(ThreadPoolRunnable r)
Executes a given Runnable on a thread in the pool, block if needed.


setDaemon

public void setDaemon(boolean b)
The default is true - the created threads will be in daemon mode. If set to false, the control thread will not be daemon - and will keep the process alive.


setMaxSpareThreads

public void setMaxSpareThreads(int maxSpareThreads)


setMaxThreads

public void setMaxThreads(int maxThreads)


setMinSpareThreads

public void setMinSpareThreads(int minSpareThreads)


setName

public void setName(String name)


setThreadPriority

public void setThreadPriority(int threadPriority)
Sets the thread priority for current and future threads in this pool.

Parameters:
threadPriority - The new priority


shutdown

public void shutdown()
Stop the thread pool


start

public void start()


threadStatusString

public String threadStatusString()
Debug display of the stage of each thread. The return is html style, for display in the console ( it can be easily parsed too )

Returns:


Copyright B) 2000-2003 Apache Software Foundation. All Rights Reserved.