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.
Another characteristic of this thread pool is that it is fixed in size at the
time of construction. All the threads are started, and then each goes into a
wait state until a task is assigned to it. If all the threads in the pool are
currently assigned a task, the pool is empty and new requests (tasks) will
have to wait before being scheduled to run. This is a way to put an upper
bound on the amount of resources any pool can use up.
In future, this class may be enhanced to provide support growing the size of
the pool at runtime to facilitate dynamic tuning.
|