queueuserworkitem(线程池ThreadPool中QueueUserWorkItem的使用)

发布时间:2025-12-10 19:17:01 浏览次数:16

线程池ThreadPool中QueueUserWorkItem的使用-

线程池ThreadPool中QueueUserWorkItem的使用先看代码://设置可以同时处于活动状态的线程池的请求数目。boolpool=ThreadPool.SetMaxThreads(8,8);if(pool){ThreadPool.QueueUserWorkItem(o=>this.DoSomethingLong(“参数1”));ThreadPool

先看代码:

                 //设置可以同时处于活动状态的线程池的请求数目。                 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"));                };

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

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

可以看出,先运行了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"));                };                           }

运行结果:

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

总结一下

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

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477