C# ThreadPool之QueueUserWorkItem使用案例详解

作者:小目标一个亿 时间:2022-08-10 03:49:30 

先看代码:


//设置可以同时处于活动状态的线程池的请求数目。
bool pool = ThreadPool.SetMaxThreads(8, 8);
if (pool) {
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数1"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数2"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数3"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数4"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数5"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数6"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数7"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数8"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数9"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数10"));
   ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数11"));
};

上面代码先设置线程池中最大并发量为8个,然后通过QueueUserWorkItem向线程池中添加11个方法,运行,输出结果:

C# ThreadPool之QueueUserWorkItem使用案例详解

可以看出,先运行了8个,当有一个任务结束后线程池中有空闲线程时,排队的下一个任务才会执行,

把最大并发量改成9试试:


{
   //设置可以同时处于活动状态的线程池的请求数目。
   bool pool = ThreadPool.SetMaxThreads(9, 9);
   if (pool) {
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数1"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数2"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数3"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数4"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数5"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数6"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数7"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数8"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数9"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数10"));
       ThreadPool.QueueUserWorkItem(o => this.DoSomethingLong("参数11"));
   };
}

运行结果:

C# ThreadPool之QueueUserWorkItem使用案例详解

果然没错,这次是先执行9个,当有空闲线程时再执行下一个

总结一下

QueueUserWorkItem:将方法排入队列以便执行。 此方法在有线程池线程变得可用时执行。

来源:https://blog.csdn.net/wwwwerewrew/article/details/106049335

标签:C#,ThreadPool
0
投稿

猜你喜欢

  • MyBatis源码解析——获取SqlSessionFactory方式

    2023-04-03 15:09:14
  • 浅谈十个常见的Java异常出现原因

    2022-10-04 07:58:50
  • c#滚动字幕动画窗体制作步骤

    2023-12-06 20:12:58
  • Java中switch的三种用法方式小结

    2023-11-24 03:40:21
  • C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比

    2021-07-25 17:00:04
  • asp.net页面中如何获取Excel表的内容

    2022-11-18 11:40:31
  • VisualStudio2019安装C#环境的实现方法

    2021-11-02 18:36:31
  • C# DataGridView绑定数据源的方法

    2023-06-30 00:26:39
  • java实现KFC点餐小程序

    2022-12-11 21:06:51
  • Java中常用的设计模式之工厂模式详解

    2021-07-04 15:33:37
  • java结合email实现自动推送功能

    2023-07-09 00:16:43
  • Java实现经典游戏Flappy Bird的示例代码

    2022-03-28 07:05:37
  • ThreadLocal使用案例_动力节点Java学院整理

    2021-06-08 09:57:15
  • 深入理解java内置锁(synchronized)和显式锁(ReentrantLock)

    2023-11-19 00:10:57
  • Java快速排序QuickSort(实例)

    2021-12-22 21:47:42
  • java实现表单必填参数验证的方法

    2023-06-16 21:41:24
  • mybatis-plus 如何使用雪花算法ID生成策略

    2023-04-06 07:15:38
  • 教你如何用好 Java 中的枚举

    2022-11-03 10:31:21
  • 在IDEA中maven配置MyBatis的流程详解

    2021-08-26 17:02:27
  • Android studio 3.5.2安装图文教程详解

    2022-06-27 19:15:46
  • asp之家 软件编程 m.aspxhome.com