线程池ThreadPool中QueueUserWorkItem的使用

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

先看代码:

                 //设置可以同时处于活动状态的线程池的请求数目。 
                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个方法,运行,输出结果:

线程池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"));
                };
               
            }

运行结果:

线程池ThreadPool中QueueUserWorkItem的使用

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

 

总结一下

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189860.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【值得收藏】一份非常完整的Mysql规范

    做一个积极的人编码、改bug、提升自己我有一个乐园,面向编程,春暖花开!本文转载在,在原有内容基础上结合阿里巴巴Java开发手册中Mysql数据库章节的介绍,加上自己的理解和说明,整理而成。涉及的内容较多,建议收藏后仔细阅读!一、数据库命名规范数据库的表名、字段名必须使用小写字母或数字(不要使用驼峰哦),并禁止出现数字开头,禁止两个下划线中间只出现数字。正例:aliyun _ …

    2022年2月28日
    36
  • bacitracin_bababadalgharaghtakamminarronn

    bacitracin_bababadalgharaghtakamminarronn
     BalunTransformers
     ABalunisadevicewhichconverts balancedimpedanceto unbalancedandviceversa.Inaddition,balunscanalsoprovideimpedancetransformation,hencethenameBalunTransformers.
    Thefollowingsectionsdescribethep

    2025年8月11日
    5
  • 误删Oracle中数据快速恢复方式

    误删Oracle中数据快速恢复方式在开发或者维护过程存在需要对Oracle数据的恢复工作,下面提供几种方式:1.数据查询(20分钟前)select*fromTABLE_NAMEasoftimestampsysdate-20/1440;2.更新数据恢复2.1清空当前表,恢复20分钟前数据(不推荐)DELETETABLE_NAMEwhere1=1;COMMIT;inserti…

    2022年7月17日
    15
  • javaweb转发和重定向的区别_servlet转发和重定向

    javaweb转发和重定向的区别_servlet转发和重定向客户首先发送一个请求到服务器端,服务器端发现匹配的servlet,并指定它去执行,当这个servlet执行完之后,它要调用getRequestDispacther()方法,把请求转发给指定的student_list.jsp,整个流程都是在服务器端完成的,而且是在同一个请求里面完成的,因此servlet和jsp共享的是同一个request,在servlet里面放的所有东西,在student_list中都能取出来,因此,student_list能把结果getAttribute()出来,getAttribute(

    2025年10月3日
    4
  • redis——持久化方式RDB与AOF分析

    redis——持久化方式RDB与AOF分析

    2021年11月4日
    38
  • mapGetters工具函数

    mapGetters工具函数来自https://vuefe.cn/vuex/getters.html#mapGetters-工具函数mapGetters工具函数会将store中的getter映射到局部计算属性中。import{mapGetters}from‘vuex’exportdefault{computed:{…mapGetters([//使用对象扩展操作符把getter混入到comp

    2022年6月2日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号