Thursday, June 9, 2011


What Is Thread Pooling?
Thread pooling refers to a technique where a pool of worker threads is created and managed by the application. When a new job arrives, instead of creating a new thread to service it, it's queued by the thread-pool manager and dispatched later to one of the available worker threads. The thread-pool manager manages the number of active worker threads based on available resources as well as load considerations, adding new threads to the pool or freeing some worker threads in response to the number of outstanding requests. The primary goals of thread pooling are managing the number of active threads in the system and reducing the overhead of creating new threads by reusing threads from a pool.
Pool Management
Various management methods exist for the pools. You can shutdown() the pool, which will reject any future submissions but complete processing of in-process executions and even those that had not yet started but were submitted before the shutdown was initiated. You can also more aggressively perform a shutdownNow(). This will also prevent any future submissions, but it has a few different, notable behaviours. It will not start execution of submitted but unstarted tasks. They will be in the returned list. It will also attempt to stop, or more precisely, Thread.interrupt() currently executing tasks. This is a best effort with no guarantee that these tasks will be successfully interrupted.

Thread Pools Implementation
You can either provide your own implementation of thread pools or use java's implementation. As discussed below, you can have following implementations in Java:
  • A thread pool implementation is provided as instance of ExecutorService class,
  • you can put in different implementations of BlockingQueue to specify different queue behavior such as queue bounds or priority ordering

Implementation of Thread Pools via ExecutorService
Implementation of Thread Pool via Executor Service is shown here.

Implementation of ThreadPools via BlockingQueue
Implementation of Thread Pools via BlockingQueue is shown here.

Benefits of Thread Pooling

  • It saves the machine work of creating a new thread.
  • Single thread can recycle again and again.
  • The size of thread pool is given in starting .We can expand on the designs presented in this chapter to include a method to support growing the size of the pool at runtime if you need this kind of dynamic tuning.
  • Response time can be quick.

Risks of using thread pools

  • High Rejection rate - Suppose if an task is rejected because the thread pool is empty. There will be high rejection rate.Or if task on wait state then waiting time would be too long. Sometime there is problem of deadlock also. 
  • Contention - If you have a very large memory, and threads equivalent to that, you are contending the threads, though you have resources. So there threadlocal will be a good idea to use.

No comments:

Post a Comment