Infosys Microsoft Alliance and Solutions blog

« WPF - Animate with less code | Main | Inconsistent Behavior between Assembly.Load overloads »

How many Threads have I got?

When working with multi-threaded applications, we all know that we should usually try and work with the ThreadPool class as the first option. In case we have a case where the work done by a thread is long (couple of seconds), we should then look at creating a new thread to avoid starving threads from the pool and behave like a good .net managed citizen.

However there have been issues with this due to the limit of 25 threads/CPU with .NET 1.1, especially for web based applications. The default limit for minimum threads was also set to be equal to #CPU. Note only this caused lot of contention issues, but also required people to play around with the values in the ASP.NET configuration files.

There have been recommendations by MS on what are good values, but it also later prompted MS to add new APIs to .NET CLR 1.1 that allowed one to configure the minimum threads programmatically. This had also became necessary since the addition of new threads to the pool is throttled and takes 500 ms. With the ability to set the minimum threads early on, one could avoid the time delays due to throttling.

Ability to set the minimum threads by itself isn't sufficient since one is still limited by the max threads. One could query this value using the ThreadPool.GetMaxThreads API. To give more flexibility to the developers, .NET 2.0 introduced a new API - ThreadPool.SetMaxThreads.

However with the release of .NET 2.0 SP1, the ThreadPool.SetMaxThread API will become less useful since the default value of threads in the pool has now been increased to 250 threads/CPU.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter