public class ThreadPool extends Object implements LifeCycle, Serializable
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.
Modifier and Type | Class and Description |
---|---|
static class |
ThreadPool.PoolThread
Pool Thread class.
|
Modifier and Type | Field and Description |
---|---|
static String |
__DAEMON |
static String |
__PRIORITY |
Constructor and Description |
---|
ThreadPool() |
Modifier and Type | Method and Description |
---|---|
int |
getIdleThreads()
Get the number of idle threads in the pool.
|
int |
getMaxIdleTimeMs()
Get the maximum thread idle time.
|
int |
getMaxThreads()
Set the maximum number of threads.
|
int |
getMinThreads()
Get the minimum number of threads.
|
String |
getName() |
String |
getPoolName()
Deprecated.
use getName()
|
int |
getThreads()
Get the number of threads in the pool.
|
int |
getThreadsPriority()
Get the priority of the pool threads.
|
protected void |
handle(Object job)
Handle a job.
|
boolean |
isDaemon()
Delegated to the named or anonymous Pool.
|
boolean |
isStarted()
Is the pool running jobs.
|
void |
join() |
void |
run(Object job)
Run job.
|
void |
setDaemon(boolean daemon)
Delegated to the named or anonymous Pool.
|
void |
setMaxIdleTimeMs(int maxIdleTimeMs)
Set the maximum thread idle time.
|
void |
setMaxStopTimeMs(int ms)
Deprecated.
maxIdleTime is used instead.
|
void |
setMaxThreads(int maxThreads)
Set the maximum number of threads.
|
void |
setMinThreads(int minThreads)
Set the minimum number of threads.
|
void |
setName(String name)
Set the Pool name.
|
void |
setPoolName(String name)
Deprecated.
use setName(String)
|
void |
setThreadsPriority(int priority)
Set the priority of the pool threads.
|
void |
shrink() |
void |
start()
Start the LifeCycle.
|
void |
stop()
Stop the ThreadPool.
|
protected void |
stopJob(Thread thread,
Object job)
Stop a Job.
|
public static final String __DAEMON
public static final String __PRIORITY
public String getName()
public void setName(String name)
name
- Name of the Pool instance this ThreadPool uses or null for an anonymous private
pool.public String getPoolName()
public void setPoolName(String name)
public boolean isDaemon()
public void setDaemon(boolean daemon)
public boolean isStarted()
public int getThreads()
getIdleThreads()
public int getIdleThreads()
getThreads()
public int getMinThreads()
setMinThreads(int)
public void setMinThreads(int minThreads)
minThreads
- minimum number of threadsgetMinThreads()
public int getMaxThreads()
setMaxThreads(int)
public void setMaxThreads(int maxThreads)
maxThreads
- maximum number of threads.getMaxThreads()
public int getMaxIdleTimeMs()
setMaxIdleTimeMs(int)
public void setMaxIdleTimeMs(int maxIdleTimeMs)
maxIdleTimeMs
- Max idle time in ms.getMaxIdleTimeMs()
public int getThreadsPriority()
public void setThreadsPriority(int priority)
priority
- the new thread priority.public void setMaxStopTimeMs(int ms)
public void start() throws Exception
LifeCycle
public void stop() throws InterruptedException
stop
in interface LifeCycle
InterruptedException
- Stopping a lifecycle is rarely atomic
and may be interrupted by another thread. If this happens
InterruptedException is throw and the component will be in an
indeterminant state and should probably be discarded.public void join()
public void shrink() throws InterruptedException
InterruptedException
public void run(Object job) throws InterruptedException
job
- If the job is derived from Runnable, the run method is called, otherwise it is
passed as the argument to the handle method.InterruptedException
protected void handle(Object job) throws InterruptedException
job
- The job to execute.InterruptedException
protected void stopJob(Thread thread, Object job)
thread
- The thread allocated to the job, or null if no thread allocated.job
- The job object passed to run.Copyright © 2013. All rights reserved.