What is Thread Pooling? Why is it needed?

Thread pooling is the process of creating a collection of threads during the initialization of a multithreaded application, and then reusing those threads for new tasks as and when required, instead of creating new threads. The number of threads for the process is usually fixed depending on the amount of memory available, and the needs of the application. However, it might be possible to increase the number of available threads. Each thread in the pool is given a task and, once that task has completed, the thread returns to the pool and waits for the next assignment.

The Concept of Thread Pooling

One of the major problems affecting the responsiveness of a multithreaded application is the time involved in spawning threads for each task.

For example, a web server is a multithreaded application that can service several client requests simultaneously. Let’s suppose that ten clients are accessing the web server at the same time:

  • If the server operates a one thread per client policy, it will spawn ten new threads to service these clients, which entails the overhead of first creating those threads and then of managing them throughout their lifetime. It’s also possible that the machine will run out of resources at some point.
  • Alternatively, if the server uses a pool of threads to satisfy those requests, then it will save the time involved in the spawning of those threads each time a request from a client comes in. It can manage the number of threads created, and can reject client requests if it is too busy to handle them. This is exactly the concept behind thread pooling.

The factors affecting the threading design of a multithreaded application are:

  • The responsiveness of the application
  • The allocation of thread management resources
  • Resource sharing
  • Thread synchronization

Thread pooling is essential in multithreaded applications for the following reasons.

  • Thread pooling improves the response time of an application as threads are already available in the thread pool waiting for their next assignment and do not need to be created from scratch
  • Thread pooling optimizes the thread time slices according to the current process running in the system
  • ┬áThread pooling enables us to start several tasks without having to set the properties for each thread
  • Thread pooling enables us to pass state information as an object to the procedure arguments of the task that is being executed
  • Thread pooling can be employed to fix the maximum number of threads for processing a particular request

Tags: ,

Comments are closed.