C# 的SmartThreadPool线程使用

1、为什么需要使用线程池(Thread Pool)
减少线程间上下文切换。线程执行一定的时间片后,系统会自动把cpu切换给另一个线程使用,这时还需要保存当前的线程上下文状态,并加载新线程的上下文状态。当程序中有大量的线程时,每个线程分得的时间片会越来越少,可能会出现线程未处理多少操作,就需要切换到另一线程,这样频繁的线程间上下文切换会花费大量的cpu时间。
减少内存占用。系统每创建一条物理线程,需要大概花费1MB的内存空间,许多程序喜欢先创建多条物理线程,并周期轮询来处理各自的任务,这样既消耗了线程上下文切换的时间,还浪费了内存。这些任务可能只需要一条线程就能满足要求。假如某一任务需要执行较长的周期,线程池还可以自动增加线程,并在空闲时,销毁线程,释放占用的内存。
2、为什么不使用.Net默认的线程池
.Net默认的线程池(ThreadPool)是一个静态类,所以是没办法自己创建一个新的程序池的。默认的线程池与应用程序域(AppDomain)挂钩,一个AppDomain只有一个线程池。假如在线程池中执行了一个周期较长的任务,一直占用着其中一个线程,可能就会影响到应用程序域中的其他程序的性能。例如,假如在Asp.Net的线程池中执行一个周期较长的任务,就会影响请求的并发处理能力(线程池默认有个最大线程数)。
3、SmartThreadPool特性和优点
SmartThreadPool特性如下:

  • 池中的线程数量会根据负载自动增减
  • 任务异步执行后可以返回值
  • 处于任务队列中未执行的任务可以取消
  • 回调函数可以等待多个任务都执行完成后再触发
  • 任务可以有优先级(priority)
  • 任务可以分组
  • 支持泛型Action 和 Func
  • 有性能监测机制
    4、实列使用
    4.1、实例化时参数解释
STPStartInfo stp = new STPStartInfo();
stp.CallToPostExecute = CallToPostExecute.Always;
stp.DisposeOfStateObjects = true;
stp.IdleTimeout = 300;//300s
stp.MaxWorkerThreads = 15;
stp.FillStateWithArgs = true;
stp.MinWorkerThreads = 5;
stp.PostExecuteWorkItemCallback = delegate(IWorkItemResult wir) 
{ 
    MessageBox.Show("ok" + wir.Result); };
    stp.StartSuspended = true;
}
m_hThreadPool = new SmartThreadPool(stp);

4.2、SmartThreadPool的部分解释

SmartThreadPool smartThreadPool = new SmartThreadPool();
smartThreadPool.Cancel();
smartThreadPool.Cancel(true);
smartThreadPool.Concurrency = 25;
smartThreadPool.CreateWorkItemsGroup(3);
smartThreadPool.Join(new Action[] { new Action(Test) });
smartThreadPool.MaxThreads = 25;
smartThreadPool.MinThreads = 0;
smartThreadPool.Name = "StartThreadPool";
smartThreadPool.OnIdle += new WorkItemsGroupIdleHandler(smartThreadPool_OnIdle);
smartThreadPool.OnThreadInitialization += new ThreadInitializationHandler(smartThreadPool_OnThreadInitialization);
smartThreadPool.OnThreadTermination += new ThreadTerminationHandler(smartThreadPool_OnThreadTermination);
smartThreadPool.Pipe<object>(new object(), new Action<object>[] { new Action<object>(Test) });
STPStartInfo stpStartInfo = smartThreadPool.STPStartInfo;smartThreadPool.WaitingCallbacks;
WIGStartInfo wigStartInfo = smartThreadPool.WIGStartInfo;

4.3、 最简单的使用方法

// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
 // 执行任务
smartThreadPool.QueueWorkItem(() =>
{
     Console.WriteLine("Hello World!");
});

4.4、带返回值的任务:

// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
// 执行任务
var result = smartThreadPool.QueueWorkItem(() =>
{
    var sum = 0;
    for (var i = 0; i < 10; i++)
        sum += i;
 
    return sum;
});
// 输出计算结果
Console.WriteLine(result.Result);

4.5、等待多个任务执行完成:

// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
// 执行任务
var result1 = smartThreadPool.QueueWorkItem(() =>
{
    //模拟计算较长时间
    Thread.Sleep(5000);
    return 3;
});
var result2 = smartThreadPool.QueueWorkItem(() =>
{
    //模拟计算较长时间
    Thread.Sleep(3000);
    return 5;
});
bool success = SmartThreadPool.WaitAll(
    new IWorkItemResult[] { result1, result2 });
if (success)
{
    // 输出结果
    Console.WriteLine(result1.Result);
    Console.WriteLine(result2.Result);
}
posted @   qiutian-hao  阅读(347)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示