public class ThreadPool extends Object
The Pool will automatically grow as required up to its maximum pool size. The Pool will be automatically shrink by trimming threads that have been idle for some time.
| Constructor and Description |
|---|
ThreadPool(String poolName,
boolean isDaemon,
Integer threadPriority)
Create the ThreadPool.
|
ThreadPool(String poolName,
boolean isDaemon,
Integer threadPriority,
int minSize,
int maxSize,
long maxIdleMillis) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
assign(Runnable work,
boolean addToQueueIfFull)
Assign a task to the thread pool, specifing the options to wait or queue
the task if the pool is fully busy and can't grow.
|
static ThreadPool |
createThreadPool(String poolName)
Create and return a ThreadPool reading configuration from GlobalProperties.
|
int |
getBusyCount()
Return the number of currently busy threads.
|
PooledThread |
getBusyThread(String threadName)
Find a thread using its name from the busy list.
|
Iterator<PooledThread> |
getBusyThreads()
Return an Iterator of PooledThread that are currently running.
|
long |
getMaxIdleTime()
Return the maximum amount of time in millis that Threads can be idle before
they are trimmed.
|
int |
getMaxSize()
Return the maximum size this pool can grow to.
|
int |
getMinSize()
Return the minimum size the pool should maintain.
|
String |
getName()
Return the name of the thread pool.
|
PooledThread |
interrupt(String threadName)
Interrupt a named thread that is currently busy.
|
boolean |
isStopping()
Return true if the pool is shutting down.
|
protected void |
maintainPoolSize()
Trim or grow the pool leaving at least min free.
|
protected void |
removeThread(PooledThread thread)
Remove the thread from the pool.
|
protected void |
returnThread(PooledThread thread)
fired when a Thread from the pool has finished, and can be put back into
the pool.
|
void |
setMaxIdleTime(long maxIdleTime)
Set the maxiumium amount of time in millis that Threads can be idle before
they are trimed.
|
void |
setMaxSize(int maxSize)
Set the maximum size the pool should grow to.
|
void |
setMinSize(int minSize)
Set the minimum size the pool should try to maintain.
|
void |
shutdown()
Shutdown the threadpool stopping all the threads.
|
int |
size()
Return the total number of busy and free threads in the pool.
|
public ThreadPool(String poolName, boolean isDaemon, Integer threadPriority, int minSize, int maxSize, long maxIdleMillis)
public static ThreadPool createThreadPool(String poolName)
public boolean isStopping()
public String getName()
public void setMinSize(int minSize)
public int getMinSize()
public void setMaxSize(int maxSize)
public int getMaxSize()
public int size()
public int getBusyCount()
public boolean assign(Runnable work, boolean addToQueueIfFull)
When the pool is fully busy...
addToQueue=true -> work is added to queue, returns false
addToQueue=false -> work is not done or queued, returns false
work - the runnable work to do.addToQueueIfFull - If the pool is maxed out and this is true then it queues the
Runnable.protected void removeThread(PooledThread thread)
protected void returnThread(PooledThread thread)
public Iterator<PooledThread> getBusyThreads()
public void shutdown()
protected void maintainPoolSize()
public PooledThread interrupt(String threadName)
Returns the thread that was interrupted or null if the thread was not found. If the thread was interrupted then it will automatically be stopped and removed from the pool.
Note that it may take some time to actually interrupt the thread so an immediate test to see if the thread stopped will probably be wrong.
ThreadPool test = ThreadPoolManager.getThreadPool("test");
PooledThread pt = test.interrupt("test.1");
if (pt == null) {
// the thread was not found, perhaps finished?
} else {
// give interrupt a little time to execute
Thread.sleep(1000);
boolean hasStopped = pt.isStopped();
//..
}
public PooledThread getBusyThread(String threadName)
public long getMaxIdleTime()
public void setMaxIdleTime(long maxIdleTime)
Copyright © 2014. All Rights Reserved.