org.apache.tomcat.util.threads

Class ThreadPool

public class ThreadPool extends 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?

Author: Gal Shachor Yoav Shapira

Nested Class Summary
static classThreadPool.ControlRunnable
A Thread object that executes various actions ( ThreadPoolRunnable ) under control of ThreadPool
static classThreadPool.MonitorRunnable
Periodically execute an action - cleanup in this case
static interfaceThreadPool.ThreadPoolListener
Interface to allow applications to be notified when a threads are created and stopped.
Field Summary
protected intcurrentThreadCount
protected intcurrentThreadsBusy
protected booleanisDaemon
protected Vectorlisteners
protected intmaxSpareThreads
protected intmaxThreads
protected intminSpareThreads
protected ThreadPool.MonitorRunnablemonitor
static intMAX_SPARE_THREADS
static intMAX_THREADS
static intMAX_THREADS_MIN
static intMIN_SPARE_THREADS
protected Stringname
Name of the threadpool
protected ThreadPool.ControlRunnable[]pool
protected intsequence
Sequence.
protected booleanstopThePool
protected intthreadPriority
Thread priority.
protected Hashtablethreads
The threads that are part of the pool.
static intWORK_WAIT_TIMEOUT
Constructor Summary
ThreadPool()
Constructor.
Method Summary
voidaddThread(Thread t, ThreadPool.ControlRunnable cr)
voidaddThreadPoolListener(ThreadPool.ThreadPoolListener tpl)
protected voidadjustLimits()
protected voidcheckSpareControllers()
Called by the monitor thread to harvest idle threads.
static ThreadPoolcreateThreadPool(boolean jmx)
Create a ThreadPool instance.
intgetCurrentThreadCount()
intgetCurrentThreadsBusy()
booleangetDaemon()
static intgetDebug()
intgetMaxSpareThreads()
intgetMaxThreads()
intgetMinSpareThreads()
ThreadPool.MonitorRunnablegetMonitor()
StringgetName()
intgetSequence()
String[]getThreadParam()
Return an array with the current "param" ( XXX better name ?
intgetThreadPriority()
Returns the priority level of current and future threads in this pool.
EnumerationgetThreads()
String[]getThreadStatus()
Return an array with the status of each thread.
intincSequence()
booleanisDaemon()
protected voidnotifyThreadEnd(ThreadPool.ControlRunnable c)
Inform the pool that the specific thread finish.
protected voidopenThreads(int toOpen)
Create missing threads.
voidremoveThread(Thread t)
protected voidreturnController(ThreadPool.ControlRunnable c)
Returns the thread to the pool.
voidrun(Runnable r)
voidrunIt(ThreadPoolRunnable r)
Executes a given Runnable on a thread in the pool, block if needed.
voidsetDaemon(boolean b)
The default is true - the created threads will be in daemon mode.
voidsetMaxSpareThreads(int maxSpareThreads)
voidsetMaxThreads(int maxThreads)
voidsetMinSpareThreads(int minSpareThreads)
voidsetName(String name)
voidsetThreadPriority(int threadPriority)
Sets the thread priority for current and future threads in this pool.
voidshutdown()
Stop the thread pool
voidstart()
StringthreadStatusString()
Debug display of the stage of each thread.

Field Detail

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

MAX_SPARE_THREADS

public static final int MAX_SPARE_THREADS

MAX_THREADS

public static final int MAX_THREADS

MAX_THREADS_MIN

public static final int MAX_THREADS_MIN

MIN_SPARE_THREADS

public static final int MIN_SPARE_THREADS

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

WORK_WAIT_TIMEOUT

public static final int WORK_WAIT_TIMEOUT

Constructor Detail

ThreadPool

public ThreadPool()
Constructor.

Method Detail

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: The params of all threads

getThreadPriority

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

Returns: The priority

getThreads

public Enumeration getThreads()

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: The status of all threads

incSequence

public int incSequence()

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

Throws: IllegalArgumentException If the specified priority is less than Thread.MIN_PRIORITY or more than Thread.MAX_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: The thread status display

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.