Using Threads Versus Using Thread Pools

ThreadPools reduces thread creation and termination overhead because it uses single thread from the thread pool to execute multiple tasks serially.Whereas in the normal senario in a multithreaded applcation they are executed on seperat thread parallely.To understand it better, lets assume a senario, Suppose we have 5 tasks that take 1ms time each to execute and 1ms for creation and 1ms for termination.

In case of using ThreadPool(Suppose SetMaxThreads=1)
The thread creation/termination overhead will be 2ms because single thread is created and the work done will be of 5ms.

In case of using Thread
The thread creation/termination overhead will be 10ms because 5 threads will be created/terminated seperatly and the total work done will be of 5ms.

So the conclusion is that in case of executing multiple breif tasks ThreadPool is more sutiable and in case of executing lengthy tasks Threads are better option.

Reference : http://blogs.msdn.com/oldnewthing/archive/2007/04/03/2014992.aspx

No comments: