org.mortbay.util
Class ThreadPool
java.lang.Object
org.mortbay.util.ThreadPool
- LifeCycle, Serializable
public class ThreadPool
extends java.lang.Object
A pool of threads.
Avoids the expense of thread creation by pooling threads after their run methods exit for reuse.
If the maximum pool size is reached, jobs wait for a free thread.
Idle threads timeout and terminate until the minimum number of threads are running.
This implementation uses the run(Object) method to place a job on a queue, which is read by the
getJob(timeout) method. Derived implementations may specialize getJob(timeout) to obtain jobs
from other sources without queing overheads.
$Id: ThreadPool.java,v 1.41 2005/08/13 00:01:28 gregwilkins Exp $- Juancarlo A�ez
- Greg Wilkins
__DAEMON
public static final String __DAEMON
__PRIORITY
public static final String __PRIORITY
ThreadPool
public ThreadPool()
getIdleThreads
public int getIdleThreads()
Get the number of idle threads in the pool. Delegated to the named or anonymous Pool.
getMaxIdleTimeMs
public int getMaxIdleTimeMs()
Get the maximum thread idle time. Delegated to the named or anonymous Pool.
getMaxThreads
public int getMaxThreads()
Set the maximum number of threads. Delegated to the named or anonymous Pool.
- maximum number of threads.
getMinThreads
public int getMinThreads()
Get the minimum number of threads. Delegated to the named or anonymous Pool.
- minimum number of threads.
getName
public String getName()
- The name of the ThreadPool.
getPoolName
public String getPoolName()
use getName()
getThreads
public int getThreads()
Get the number of threads in the pool. Delegated to the named or anonymous Pool.
getThreadsPriority
public int getThreadsPriority()
Get the priority of the pool threads.
- the priority of the pool threads.
handle
protected void handle(Object job)
throws InterruptedException
Handle a job. Called by the allocated thread to handle a job. If the job is a Runnable, it's
run method is called. Otherwise this method needs to be specialized by a derived class to
provide specific handling.
job
- The job to execute.
isDaemon
public boolean isDaemon()
Delegated to the named or anonymous Pool.
isStarted
public boolean isStarted()
Is the pool running jobs.
- isStarted in interface LifeCycle
- True if start() has been called.
run
public void run(Object job)
throws InterruptedException
Run job. Give a job to the pool.
job
- If the job is derived from Runnable, the run method is called, otherwise it is
passed as the argument to the handle method.
setDaemon
public void setDaemon(boolean daemon)
Delegated to the named or anonymous Pool.
setMaxIdleTimeMs
public void setMaxIdleTimeMs(int maxIdleTimeMs)
Set the maximum thread idle time. Threads that are idle for longer than this period may be
stopped. Delegated to the named or anonymous Pool.
maxIdleTimeMs
- Max idle time in ms.
setMaxStopTimeMs
public void setMaxStopTimeMs(int ms)
maxIdleTime is used instead.
Set Max Read Time.
setMaxThreads
public void setMaxThreads(int maxThreads)
Set the maximum number of threads. Delegated to the named or anonymous Pool.
maxThreads
- maximum number of threads.
setMinThreads
public void setMinThreads(int minThreads)
Set the minimum number of threads. Delegated to the named or anonymous Pool.
minThreads
- minimum number of threads
setName
public void setName(String name)
Set the Pool name. All ThreadPool instances with the same Pool name will share the same Pool
instance. Thus they will share the same max, min and available Threads. The field values of
the first ThreadPool to call setPoolName with a specific name are used for the named Pool.
Subsequent ThreadPools that join the name pool will loose their private values.
name
- Name of the Pool instance this ThreadPool uses or null for an anonymous private
pool.
setPoolName
public void setPoolName(String name)
use setName(String)
setThreadsPriority
public void setThreadsPriority(int priority)
Set the priority of the pool threads.
priority
- the new thread priority.
shrink
public void shrink()
throws InterruptedException
start
public void start()
throws Exception
Start the LifeCycle.
- start in interface LifeCycle
stop
public void stop()
throws InterruptedException
Stop the ThreadPool. New jobs are no longer accepted,idle threads are interrupted and
stopJob is called on active threads. The method then waits
min(getMaxStopTimeMs(),getMaxIdleTimeMs()), for all jobs to stop, at which time killJob is
called.
- stop in interface LifeCycle
stopJob
protected void stopJob(Thread thread,
Object job)
Stop a Job. This method is called by the Pool if a job needs to be stopped. The default
implementation does nothing and should be extended by a derived thread pool class if special
action is required.
thread
- The thread allocated to the job, or null if no thread allocated.job
- The job object passed to run.
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.