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 and Description |
---|
ThreadPool(String poolName,
int numThreads,
boolean daemon,
Debug debug)
Constructs a thread pool with the poolName and given number of threads.
|
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Destroys the thread pool.
|
String |
getName()
Returns the name of this thread pool
|
void |
run(Runnable task)
Runs the user-defined task.
|
void |
stopIdleThreads()
Stops all the idle threads in the pool.
|
String |
toString()
Returns the string representation of this thread pool that includes the
name, size and the number of currently idle threads
|
public ThreadPool(String poolName, int numThreads, boolean daemon, Debug debug) throws IllegalArgumentException
Constructs a thread pool with the poolName and given number of threads.
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.IllegalArgumentException
- if poolName is nullpublic final void run(Runnable task) throws InterruptedException
task
Runnable
should
implement toString()
to intuitively identify the task.task
- the user-defined Runnable to be scheduled for execution on
this thread poolInterruptedException
- when the thread invoking run
is interrupted.public final void stopIdleThreads()
public final void destroy()
public String toString()
public final String getName()
Copyright © 2010–2025 Open Identity Platform Community. All rights reserved.