Abstract


  • A group of fixed-number worker Thread that are reused to execute tasks submitted to Task Queue by a client. The number of Thread is usually the number of cores the system has. Less will lead to wasted CPU power, more will introduce Context Switch which has overhead, unless the task isn’t cpu-bounded
  • Commonly used in scenarios where tasks are small, numerous, and independent(stateless). Examples include web servers handling HTTP requests and applications that perform many asynchronous I/O operations

Benefits


Improved Resource Management

  • By limiting the number of Thread that can be active at any one time
  • A thread pool can help to ensure that the system does not become overwhelmed with too many concurrent threads, which can reduce performance due to Context Switch or lead to resource thrashing

Reduced Overhead

  • Creating a new thread is an expensive operation, so reusing threads can improve performance

2 coding aspects


1) Thread Safety

2) Computing Resources used by Waiting Threads

  • Make sure Thread that is idle not keep checking for tasks which will result in wasted computation

Terminologies


Task Queue

  • Tasks that need to be executed are placed in a queue
  • When a Thread becomes available, it pulls a task from the queue and executes it
  • After completing the task, the thread returns to the pool to await the next assignment