[Note: This has been fixed in .NET 3.5 SP1, read more on this post.]
Holy smokes! I thought we had figured out something significant when I posted .NET 3.5 Brings Major (Undocumented) Changes to ThreadPool where we discovered that the .NET 3.5 ThreadPool changed the allocation algorithm for adding threads from linear to logarithmic.This is bigger. [Recently updated see note below]Here's the scenario. I have a server - say in the financial sector - that must process many requests and it must get up to speed immediately. I can't pay the 500 ms warm up time for the ThreadPool (.NET 2.0) or the even slower model in .NET 2.0 SP1. What do I do? I call ThreadPool.SetMinThreads(x, x) where x < (the current max), but much higher than 2 (the default). So I might call ThreadPool.SetMinThreads(100, 100) or something like that.In .NET 1.0 - .NET 2.0 (without SP1) you would go from the warm up time model: warm up time model (.NET 2.0)To this immediate "ready" threading model.
using System;using System.Threading;namespace NewThreadPoolBehavior{ internal class Program { private static void Main(string[] args) { Console.WriteLine( "Running on " + Environment.Version ); int w, c; ThreadPool.GetMaxThreads( out w, out c ); Console.WriteLine( "Max Currently: " + w + ", " + c ); ThreadPool.GetMinThreads( out w, out c ); Console.WriteLine( "Min Currently: " + w + ", " + c ); Console.WriteLine( "Set min thread count 100? (y/n) " ); string txt = Console.ReadLine(); if ( txt == "y" ) { Console.WriteLine( "Setting min to 100" ); ThreadPool.SetMinThreads( 100, 100 ); ThreadPool.GetMinThreads( out w, out c ); Console.WriteLine( "Min Currently: " + w + ", " + c ); } UseThreadPool( 200 ); Console.ReadLine(); } private static void UseThreadPool(int count) { for ( int i = 0; i < count; i++ ) { ThreadPool.QueueUserWorkItem( delegate { SlowMethod(); } ); } } private static int concurrent = 0; private static void SlowMethod() { concurrent++; Console.WriteLine( "Starting ops (" + concurrent + " concurrent)" ); Thread.Sleep( 20000 ); Console.WriteLine( "Finished ops (" + concurrent + " concurrent)" ); concurrent--; } }}
Posted in DevelopMentor | Visual Studio |Comments [4]
A blog by Michael Kennedy
Speaking Events
About: Michael is an instructor for DevelopMentor, a .NET enthusiast, an agile pioneer, an entrepreneur, a father of three girls, a husband, a student, and a teacher.
Explore Related Content
Concepts >> azure - Workflow - RESTful - Unit Testing - web - WCF - ASP.Net - MVC - LINQ
Tools >> Visual Studio ClickOnce IIS
Type >> Screencast - Tools - Article - How To - Course
@dm_the_company @mkennedy @andrewclymer @bmaso @danamiga @georgeshep @isidore_us @jason_diamond @markblomsma @marksm @mauricedb @pierrenallet @richardblewett @tonysneed @wallacekelly
Sign In