Thread pool pattern
|
In the thread pool pattern in programming, a number of N threads are created to perform a number of M tasks, usually organized in a queue. Typically, N << M. As soon as a thread completes its task, it will request the next task from the queue until all tasks have been completed. The thread can then terminate, or sleep until there are new tasks available.
The number of threads used (N) is a parameter that can be tuned to provide the best performance.
The advantage of using a Thread Pool over creating a new thread for each task, is that thread creation and destruction overhead is negated, which will result in better performance, and better system stability.
When implementing this pattern, the programmer should ensure thread-safety of the queue.