Class ThreadPool
- java.lang.Object
-
- com.iplanet.services.util.ThreadPool
-
public final class ThreadPool extends Object
ThreadPool is a generic thread pool that manages and recycles threads instead of creating them everytime some task needs to be run on a different thread. Thread pooling saves the virtual machine the work of creating brand new threads for every short-lived task. In addition, it minimizes overhead associated with getting a thread started and cleaning it up after it dies. By creating a pool of threads, a single thread from the pool can be reused any number of times for different tasks.
This reduces response time because a thread is already constructed and started and is simply waiting for its next task. This is particularly useful when using many short-lived tasks. This may not be useful for long-lived tasks.
In future, this class may be enhanced to provide support growing the size of the pool at runtime to facilitate dynamic tuning.
-
-
Constructor Summary
Constructors Constructor Description ThreadPool(String poolName, int numThreads, boolean daemon, Debug debug)Constructs a thread pool with the poolName and given number of threads.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddestroy()Destroys the thread pool.StringgetName()Returns the name of this thread poolvoidrun(Runnable task)Runs the user-defined task.voidstopIdleThreads()Stops all the idle threads in the pool.StringtoString()Returns the string representation of this thread pool that includes the name, size and the number of currently idle threads
-
-
-
Constructor Detail
-
ThreadPool
public ThreadPool(String poolName, int numThreads, boolean daemon, Debug debug) throws IllegalArgumentException
Constructs a thread pool with the poolName and given number of threads.
- Parameters:
poolName- name of the thread poolnumThreads- maximum number of threads in the thread pool.daemon- if true, all threads created will be daemon threads. If false, all threads created will be non-daemon threads.debug- Debug object to send debug messages to.- Throws:
IllegalArgumentException- if poolName is null
-
-
Method Detail
-
run
public final void run(Runnable task) throws InterruptedException
Runs the user-defined task. To enable better debugging or profiling capabilities, thetaskRunnableshould implementtoString()to intuitively identify the task.- Parameters:
task- the user-defined Runnable to be scheduled for execution on this thread pool- Throws:
InterruptedException- when the thread invokingrunis interrupted.
-
stopIdleThreads
public final void stopIdleThreads()
Stops all the idle threads in the pool. Note that these stopped threads are no longer availble for future tasks because they are returned to underlying virtual machine. Also note that none of the active threads in the pool are stopped.
-
destroy
public final void destroy()
Destroys the thread pool. This stops all the threads, active and idle, in the pool and releases all resources.
-
toString
public String toString()
Returns the string representation of this thread pool that includes the name, size and the number of currently idle threads
-
getName
public final String getName()
Returns the name of this thread pool
-
-