views:

103

answers:

3

Normally I heard that 25 threads are operating on ThreadPool.

When I execute the following :

namespace Examples.Delegates
{
    public delegate void InvokerDelegate();
    class Discussion
    {
       static void Main()
        {
            Discussion dis=new Discussion();
            InvokerDelegate MethodInvoker=dis.Foo;
            for(int i=1;i<=30;i++)
            {
                MethodInvoker.BeginInvoke(null,null);
            }

            Console.ReadKey(true);
        }

        private void Foo()
        {
            int avlThreads, avlToAsynThreads;

            ThreadPool.GetAvailableThreads
            (out avlThreads,out avlToAsynThreads);

            string Message=string.
            Format("From ThreadPool :{0} ,Thread Id :{1},Free Threads :{2}",
            Thread.CurrentThread.IsThreadPoolThread,
            Thread.CurrentThread.ManagedThreadId,
            avlThreads);
            Console.WriteLine(Message);

            Thread.Sleep(1000);
            return;
        }
    }

}

The third argument of string "Message" (i.e) avlThreads prints 490+ available threads.What is the correction should i need to do?

+3  A: 

The number of threads in the thread pool has changed significantly over time.

  • .NET 1.0/1.1/2.0/3.0: 25 worker threads per available processor. Not sure about I/O completion threads.

  • .NET 3.5/4.0: 250 worker threads per available processor, and 1000 I/O completion threads.

(I have some vague memory that it was going to change for .NET 4.0, but the MSDN provisional documentation doesn't indicate that.)

So I suspect that you're running on a dual-processor system under .NET 3.5 or 4.0.

Jon Skeet
yes mine is dual processor on .NET 3.5
+1  A: 

I think that the correction you need to do is to read the documentation. The ThreadPool by default has 250 threads per processor. In .NET Framework 1.0, 1.1 and 2.0 it was 25, in 3.5 it's 250.

Fredrik Mörk
+1  A: 

From MSDN:

The thread pool has a default size of 250 worker threads per available processor

So if your machine is dual or quad core, it is normal that you get so many available threads.

Konamiman