Well, my recent post on .NET 3.5 Brings BREAKING Changes to ThreadPool sparked quite a bit of interest in the .NET community.
But this is also something difficult to convince people of because it depends so heavily on configuration. The source code doesn’t change, the environment does.
So I’ve put together a screencast demonstrating the problem and elaborating further. If you doubt the validity of the previous post, or can’t reproduce the problem, please watch the video:
Download the video (approx 18 MB)
In the video I work with a modified version of the program. Here’s that for your enjoyment:
The application: NetThreading.exe (From Video).zip (2.59 KB)
The source code:
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 20? (y/n) " );
string txt = Console.ReadLine();
if ( txt == "y" )
{
Console.WriteLine( "Setting min to 20" );
ThreadPool.SetMinThreads( 20, 100 );
ThreadPool.GetMinThreads( out w, out c );
Console.WriteLine( "Min Currently: " + w + ", " + c );
}
UseThreadPool( 200 );
Console.ReadLine();
}
private static DateTime startTime;
private static void UseThreadPool(int count)
{
startTime = DateTime.Now;
for ( int i = 0; i < count; i++ )
{
ThreadPool.QueueUserWorkItem(
delegate { SlowMethod(); } );
}
}
private static int concurrent = 0;
private static void SlowMethod()
{
TimeSpan dt = DateTime.Now - startTime;
concurrent++;
Console.WriteLine( "Starting ops (" + concurrent + " concurrent, elapsed=" +
dt.TotalSeconds.ToString( "N3" ) + " sec.) " );
Thread.Sleep( 20000 );
Console.WriteLine( "Finished ops (" + concurrent + " concurrent)" );
concurrent--;
}
}
}