首页 > 编程知识 正文

线程池corepoolsize为0,线程池运行流程,参数,策略

时间:2023-05-05 13:56:06 阅读:58873 作者:2781

先看代码:

//设置可以同时激活的线程池的请求数。 bool pool=thread pool.setmaxthreads (8,8 ); if(pool ) thread pool.queueuserworkitem (o=this.do something long ('参数1 ' ) ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数2 ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数3 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数4 ) ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数5 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数6 ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数7 ) ); thread pool.queueuserworkitem ((o=this.do something long ) ),参数8 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数9 ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数10 ' ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数11 ' ) ); (;上面代码先设置线程池中最大并发量为8个,然后通过QueueUserWorkItem向线程池中添加11个方法,运行,输出结果:

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

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

//设置可以同时激活的线程池的请求数。 bool pool=thread pool.setmaxthreads (9,9 ); if(pool ) thread pool.queueuserworkitem (o=this.do something long ('参数1 ' ) ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数2 ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数3 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数4 ) ); thread pool.queueuserworkitem ((o=this.do something long ) )、参数5 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数6 ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数7 ) ); thread pool.queueuserworkitem ((o=this.do something long ) ),参数8 ); thread pool.queueuserworkitem ((o=this.do something long ) (参数9 ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数10 ' ) ); thread pool.queueuserworkitem ((o=this.do something long ('参数11 ' ) ); (; } 运行结果:

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

http://www.Sina.com/http://www.Sina.com /

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。