在主函数中,我编写了以下代码:
ThreadPool.SetMaxThreads(200, 200);
for (int i = 0; i < 100; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWhichWillCallSQL_test), i);
Thread.Sleep(1);
}
线程函数如下所示:
static public void ThreadWhichWillCallSQL_test(Object o1)
{
Thread.Sleep(5000);
Console.WriteLine(DateTime.Now.ToString());
return;
}
输出为:
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
你可以注意到,整个过程所有线程的完成时间持续了将近28秒,据我了解,这100个线程可能不能同时完成,也不能有那么多时间相差。
我也设置了
ThreadPool.SetMaxThreads(200, 200);
并且只分配 100 个线程,所以不应该有任何线程等待其他人停止,对吧?
这是完整的代码
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;
}
}
}
在您的示例中,线程未同时启动。在 SetMaxThreads 之后添加以下调用
ThreadPool.SetMinThreads(200, 200);
这将使线程同时启动。这是关于ThreadPool线程创建机制的事情。我建议阅读 ThreadPool 文档以了解详细信息。
若要更正确地调试它,请更新ThreadWhichWillCallSQL_test方法以添加开始时间:
static void ThreadWhichWillCallSQL_test(object o1)
{
Console.WriteLine("start:" + DateTime.Now);
Thread.Sleep(5000);
Console.WriteLine("finish: " + DateTime.Now);
}
好问题。
线程拉取中线程的执行取决于许多因素,其中一个因素是 cpu核心 . 它把线程放在每个内核中运行,一旦完成,它就会放置另一个。
示例:如果有 4 核 CPU。它同时并行放置 4 个线程,然后在线程完成任务时放置下一个线程。机制是一些这样的,不完全是这样的。 你也放了 5 秒的延迟(线程睡眠(,所以计算就像如果有 4 核:(30 线程/4 核(*5 有些不完全是这样的。
你也可以在那里看到更多信息:https://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80(.aspx