why my threadpool 's threads do not finish at same time

720 Views Asked by At

In main function , I write this code :

            ThreadPool.SetMaxThreads(200, 200);

        for (int i = 0; i < 100; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWhichWillCallSQL_test), i);
            Thread.Sleep(1);
        }

and thread function is like below :

        static public void ThreadWhichWillCallSQL_test(Object o1)
    {
        Thread.Sleep(5000);
        Console.WriteLine(DateTime.Now.ToString()); 
        return;
    }

the output is :

    5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:02 PM
5/25/2017 2:00:03 PM
5/25/2017 2:00:04 PM
5/25/2017 2:00:05 PM
5/25/2017 2:00:05 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:07 PM
5/25/2017 2:00:07 PM
5/25/2017 2:00:08 PM
5/25/2017 2:00:08 PM
5/25/2017 2:00:09 PM
5/25/2017 2:00:09 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM

you can notice that the whole process of all thread finish continue for almost 28 seconds , from my understand , these 100 thread perhaps can not finish at same time , but also can not have so much time different.

also I have set

ThreadPool.SetMaxThreads(200, 200);

and only allocate 100 threads , so should not have any thread wait for others stop , right ?

here is full code

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;

    namespace ThreadpoolDelay
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    ThreadPool.SetMaxThreads(200, 200);

                    for (int i = 0; i < 30; i++)
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWhichWillCallSQL_test), i);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                Console.WriteLine("\nPress ENTER to continue...");
                Console.Read();
            }
            public static void ThreadWhichWillCallSQL_test(Object o1)
            {
                Thread.Sleep(5000);
                Console.WriteLine(DateTime.Now.ToString());
                return;
            }

        }
    }
2

There are 2 best solutions below

1
On BEST ANSWER

The threads are not being started at the same time in your example. Add the following call right after SetMaxThreads

ThreadPool.SetMinThreads(200, 200);

This will make threads start simultaneously. It's something about ThreadPool threads creating mechanics. I recommend to read ThreadPool documentation for details.

To debug it more properly, update your ThreadWhichWillCallSQL_test method to add start time too:

    static void ThreadWhichWillCallSQL_test(object o1)
    {
        Console.WriteLine("start:"  + DateTime.Now);
        Thread.Sleep(5000);
        Console.WriteLine("finish: " + DateTime.Now);
    }
5
On

Good Question.

Execution of the threads in thread pull depend on many factor and one of the factor is cpu core . it put thread to run in each core and once that is finished it put another.

Example:if there is 4 core cpu. It put simultaneously 4 thread in parallel and then it put next threads as thread finish their task. mechanism is some like that not exactly ones. Also you put there is 5 second delay (thread sleep) so calculation is like if there is 4 core : (30 thread/4 core )*5 some like that not exactly ones.

Also you can see there for more information :https://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx