Thursday, November 3, 2011

Rules for Configuring ThreadPoolExecutor Pool Size

I came across a blog Rules of ThreadPoolExecutor Pool Size. In that blog, the author explained reasonably well how TPE creates new threads in relation to thread pool size. But the author incorrectly stated that the TPE would always fill a task queue first before creating a new thread. Thread creation is determined by queuing strategy. Choosing a direct handoff strategy will achieve the author's "user anticipated way". But there is risk in that particular way. Here is a table listing the general pros and cons of each queuing strategy:


Queuing Strategy
Example
Design Trade-off
Worst Case
Usage
Direct Handoff
SynchronousQueue
Zero task queue but can create unlimited number of threads.
OutOfMemory
For tasks that may have interdependency. For example, task i changes a global state that affects the execution of task j.
Unbounded Queue
LinkedBlokingQueue
Limit the number of threads by the max pool size but allows submission of unlimited tasks.
OutOfMemory
For tasks that are completely independent of each other.
BoundedQueue
ArrayBlockingQueue
Limit both the number of threads and the queue size.
Low throughput from imbalanced pool and queue sizes.
For burning midnight oil.



No comments:

Post a Comment