class Program
{
static void DoIt(string name)
{
Console.WriteLine($"{DateTime.Now}, Thread{name} started");
Thread.Sleep(50000);
Console.WriteLine($"{DateTime.Now}, Thread{name} done");
}
static void Main()
{
//ThreadPool.SetMinThreads(100, 100);
for (int i = 0; i < 100; i++)
{
int value = i;
ThreadPool.QueueUserWorkItem((s) =>
{
string name = value.ToString();
DoIt(name);
});
}
Console.ReadKey();
}
}
The result of program: result
2021/4/26 11:26:23, Thread2 started
2021/4/26 11:26:23, Thread0 started
2021/4/26 11:26:23, Thread1 started
2021/4/26 11:26:23, Thread3 started
2021/4/26 11:26:24, Thread4 started
2021/4/26 11:26:25, Thread5 started
2021/4/26 11:26:26, Thread6 started
2021/4/26 11:26:27, Thread7 started
2021/4/26 11:26:28, Thread8 started
2021/4/26 11:26:29, Thread9 started
Start 100 threads at the same time. The first 4 threads are very fast. The latter threads start one every second util some working threads are done.
- Why first 4 thread are fast? The cpu cores of my computer is 4. If program runs in a 8 or 32 cores computer, the first 8 or 32 threads are fast, others are slow.
- if SetMinThreads to 100, all 100 threads start fast
Why ther latter threads start so slowly if i do not SetMinThreads?