Java Thread Pooling
Creating a new thread is very resource intensive. Thread pooling should be introduced into the server application code whenever possible. With thread pooling, a set of process threads can be reused instead of creating a new thread with each client request.
The system should provide the flexibility to set the maximum number of threads or allowed growth in the pool using a configuration setting. This will minimize the chance of resource thread issues.
Thread pooling can minimize JVM memory issues by sharing threads, speedup the overall process by eliminating some of the thread creation overhead and stabilize the application availability. Thread pooling is a best practice for most system services. It is utilized by most application servers and web servers.
A Thread pool implementation has been included in the core Java JDK runtime libraries.
See the package java.util.concurrent.
JDK 1.5 and or higher introduced the Executor object. It separates thread management and creation from the rest of the application. The JDK documentation recommends the use of "Executors" in large-scale applications. The executor implementations in java.util.concurrent use thread pools. This is similar to the worker threads design pattern. The thread pool size may be fixed or expandable.
Pros
- Minimize the heap memory usage
- Minimize thread creation overhead per request
- Improve system availability
Cons
- Development cost
- Addition resources needed maintain minimum number of threads in the pool