一文读懂线程池的工作原理(故事白话文)

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文以程序员做需求的例子,比喻线程池的工作过程。以故事白话的方式展开,跟大家阐述线程池工作原理,以方便大家更好理解线程池,谢谢阅读哈~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是线程池?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是核心线程?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是阻塞队列?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是非核心线程?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是空闲存活时间?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是饱和策略?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"线程池工作原理流程图&源码概览"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"github地址,感谢每一颗Star"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/whx123/JavaHome","title":null},"content":[{"type":"text","text":"github.com/whx123/Java…"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"公众号:"},{"type":"text","marks":[{"type":"strong"}],"text":"Java斗帝"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是线程池?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"小田螺"},{"type":"text","text":" 勤勤恳恳,任劳任怨,夜以继日地工作,终于有一天,他晋升为公司的主管,负责公司日常业务。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有一天,老板找到了小田螺,“我们公司员工越来越多了,我想搞个"},{"type":"text","marks":[{"type":"strong"}],"text":"员工管理系统"},{"type":"text","text":",你那边安排一下哈,要在一个月后完成。” 小田螺拍拍胸口没问题!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因为当前公司还没有程序员,所以小田螺快马加鞭打开"},{"type":"text","marks":[{"type":"strong"}],"text":"猪八戒网"},{"type":"text","text":",提交员工管理系统需求,等待不久,"},{"type":"text","marks":[{"type":"strong"}],"text":"开发者(名字,线程A)"},{"type":"text","text":" 接单,谈好合同,开始开发,系统交付...一系列流程并且一个月过后,一个五脏俱全的员工管理系统终于完成了...老板对此大加赞赏~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"过了不久,老板再次发话,“公司越来越多人迟到了,我们再搞个"},{"type":"text","marks":[{"type":"strong"}],"text":"考勤系统"},{"type":"text","text":"吧!\"小田螺接到任务,马上又开始上猪八戒网,提需求找人开发,这次来了"},{"type":"text","marks":[{"type":"strong"}],"text":"线程B接单"},{"type":"text","text":"......"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"逝者如斯,月底了,老板又提出开发个薪酬系统需求...小田螺听了头皮发麻,one day day的,重复去网上找人开发!“为了节省成本,不如我们雇佣几个程序员(线程a,b,c),成立自己的IT技术部门吧!我们就管IT部门叫"},{"type":"text","marks":[{"type":"strong"}],"text":"线程池"},{"type":"text","text":"吧!”老板听了,一拍即合!!!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"线程池就是管理线程的池子,当有任务要处理时,不用频繁创建新线程,而是从池子拿个线程出来处理。当任务执行完,线程并不会被销毁,而是在等待下一个任务。因此可以节省资源,提高响应速度。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是核心线程?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"线程池"},{"type":"text","text":"IT部门成立后,雇佣了几个与公司有正式合同关系的员工a,b,c,"},{"type":"text","marks":[{"type":"strong"}],"text":"小田螺"},{"type":"text","text":"管他们几个正式员工做"},{"type":"text","marks":[{"type":"strong"}],"text":"核心线程"},{"type":"text","text":"。当老板提一个需求过来,小田螺就把需求分配给"},{"type":"text","marks":[{"type":"strong"}],"text":"手上没活干"},{"type":"text","text":"的线程处理..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是阻塞队列?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一天早上,老板睡眼惺忪。来到公司后,一口气提了"},{"type":"text","marks":[{"type":"strong"}],"text":"四个需求"},{"type":"text","text":",a,b,c 按顺领完任务后,发现还剩余一个需求任务。这个怎么安排呢?难道又去"},{"type":"text","marks":[{"type":"strong"}],"text":"猪八戒兼职网"},{"type":"text","text":"找人嘛?成立了"},{"type":"text","marks":[{"type":"strong"}],"text":"线程池IT部门"},{"type":"text","text":",还去找人(线程干活),会被人笑落大牙的!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"聪明的小田螺想到一个好办法,我们可以搞个DPMS需求池,把还没分配的需求,放进待完成的DPMS需求池里面吧,等到a,b,c谁先干完活,再把这个任务领走。这个DPMS需求池,我们给它取名"},{"type":"text","marks":[{"type":"strong"}],"text":"阻塞队列"},{"type":"text","text":",英文名叫"},{"type":"text","marks":[{"type":"strong"}],"text":"WorkQueue"},{"type":"text","text":"吧!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是非核心线程?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"又在一个晴空万里的午后,老板喝了一杯咖啡,闲来没事,就跑去"},{"type":"text","marks":[{"type":"strong"}],"text":"阻塞队列"},{"type":"text","text":"(DPMS需求池)看看,一看就傻帽了!!需求池堆积了几十个需求,排期都是满满的了。老板马上叫"},{"type":"text","marks":[{"type":"strong"}],"text":"小田螺进来办公室"},{"type":"text","text":",以商量如何处理这些需求任务。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“要不,我们雇佣多几个员工(搞多几个"},{"type":"text","marks":[{"type":"strong"}],"text":"核心线程"},{"type":"text","text":")?” “不行不行,公司财务"},{"type":"text","marks":[{"type":"strong"}],"text":"开销"},{"type":"text","text":"有点大!”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“要不然,我们要求业务提少点任务需求?("},{"type":"text","marks":[{"type":"strong"}],"text":"请求少点"},{"type":"text","text":")” “你是不是傻,请求少点,不是自断财路嘛?你回家想想办法先吧!!”老板放大了他的嗓门~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小田螺回家闭目让神,每天早早就睡觉,两耳不闻窗外事...终于有一天,在一个梦香里,他想到了一个好办法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“老板,我们可以去别的公司("},{"type":"text","marks":[{"type":"strong"}],"text":"外包公司"},{"type":"text","text":")雇佣几个员工(假设名字为d,e,f,g)一段时间,让它们来做"},{"type":"text","marks":[{"type":"strong"}],"text":"DPMS需求池(阻塞队列)"},{"type":"text","text":" 里面的需求。等到做完需求,再派他们回去就好啦。” 老板一听就乐了,这个方案好,心里美滋滋:"},{"type":"text","marks":[{"type":"strong"}],"text":"需求的活有人干了,公司财务又省钱,两全其美呀"},{"type":"text","text":"~ 这几个派遣来的外包员工(d,e,f,g),我们就把它叫做"},{"type":"text","marks":[{"type":"strong"}],"text":"非核心线程"},{"type":"text","text":"吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是空闲时间?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自从来了d,e,f,g外包员工("},{"type":"text","marks":[{"type":"strong"}],"text":"非核心线程"},{"type":"text","text":"),老板长舒一口气,这么多活,终于有人干了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是呢,又有一天,到了7点所谓的下班时间,老板走出办公室,发现"},{"type":"text","marks":[{"type":"strong"}],"text":"线程池"},{"type":"text","text":"IT部门的员工,都走得七七八八了。心里一怒:"},{"type":"text","marks":[{"type":"strong"}],"text":"这帮粉肠,怎么一到下班时间就跑,工作这么不饱和了"},{"type":"text","text":"?他随手点进DPMS需求池,才发现,原来需求都被做完了。。。还有一堆外包同事(非核心线程)要发工资呢,这波亏大了~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二天,小田螺被"},{"type":"text","marks":[{"type":"strong"}],"text":"秘密"},{"type":"text","text":"叫进了老板办公室,既然DPMS需求池都已经没需求了。我们准备派外包同事(非核心线程)回去吧?但是呢一般,需求一没有,就马上让他们回去("},{"type":"text","marks":[{"type":"strong"}],"text":"线程回收"},{"type":"text","text":"),如果需求一下子又来,就有点hold不住了..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“要不酱紫,我们等需求池空的时候,隔个15天还是10天,再让外包同事("},{"type":"text","marks":[{"type":"strong"}],"text":"非核心线程"},{"type":"text","text":")回去吧?” 这个定义的15天或者10天,就是"},{"type":"text","marks":[{"type":"strong"}],"text":"线程空闲存活时间"},{"type":"text","text":"啦"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什么是饱和策略?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在临近双11的时候,不仅老板提了良多需求,新来的运营小姐姐们,也提了好多好多的需求。新需求如源头活水,滚滚的来~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先呢,"},{"type":"text","marks":[{"type":"strong"}],"text":"线程池"},{"type":"text","text":"IT部门a,b,c三个正式员工(核心线程)都忙于处理需求("},{"type":"text","marks":[{"type":"strong"}],"text":"请求"},{"type":"text","text":"),接着,DPMS需求池("},{"type":"text","marks":[{"type":"strong"}],"text":"阻塞队列"},{"type":"text","text":")也被挤满了,最后呢,连d,e,f,g外包同事("},{"type":"text","marks":[{"type":"strong"}],"text":"非核心线程"},{"type":"text","text":")也忙得不可开交。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这时候,需求还是做不完,怎么办呢?双11赶着上线呢?小田螺愁眉苦脸,从"},{"type":"text","marks":[{"type":"strong"}],"text":"潮起愁到潮落"},{"type":"text","text":"..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"没办法了,只能动用"},{"type":"text","marks":[{"type":"strong"}],"text":"饱和策略"},{"type":"text","text":"啦。比如"},{"type":"text","marks":[{"type":"strong"}],"text":"丢弃需求任务"},{"type":"text","text":"?"},{"type":"text","marks":[{"type":"strong"}],"text":"抛异常,告诉老板别加需求了"},{"type":"text","text":"?"},{"type":"text","marks":[{"type":"strong"}],"text":"丢弃需求池最老的需求任务"},{"type":"text","text":"?还是"},{"type":"text","marks":[{"type":"strong"}],"text":"交给提需求的人自己处理"},{"type":"text","text":"?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后老板决定,"},{"type":"text","marks":[{"type":"strong"}],"text":"拒绝再提新的需求"},{"type":"text","text":",于是"},{"type":"text","marks":[{"type":"strong"}],"text":"线程池"},{"type":"text","text":"IT部门还是正常运行~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"线城池的饱和策略事件,主要有四种类型"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AbortPolicy(抛出一个异常,默认的)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DiscardPolicy(新提交的任务直接被抛弃)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DiscardOldestPolicy(丢弃队列里最老的任务,将当前这个任务继续提交给线程池)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CallerRunsPolicy(交给线程池调用所在的线程进行处理,即将某些任务回退到调用者)"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"线程池工作原理流程图 & 源码概览"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"故事讲完啦,再复习下线程池工作流程图吧~"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1b/1bb61e8a4074510f9ffd6ac3d9546b78.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有兴趣的朋友,源码也看下吧~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" if (command == null)\n throw new NullPointerException();\n int c = ctl.get();\n //判断当前活跃线程数是否小于corePoolSize\n if (workerCountOf(c) < corePoolSize) {\n //如果小于,则调用addWorker创建线程执行任务\n if (addWorker(command, true))\n return;\n c = ctl.get();\n }\n //如果大于等于corePoolSize,则将任务添加到workQueue队列。\n if (isRunning(c) && workQueue.offer(command)) {\n int recheck = ctl.get();\n if (! isRunning(recheck) && remove(command))\n reject(command);\n else if (workerCountOf(recheck) == 0)\n addWorker(null, false);\n }\n //如果放入workQueue队列失败,则创建非核心线程执行任务 \n else if (!addWorker(command, false))\n //(如果这时创建线程失败(当前线程数大于等于maximumPoolSize时))\n 调用reject拒绝接受任务\n reject(command);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"公众号"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"欢迎关注公众号:【Java斗帝】"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"欢迎大家关注,持续推出干货~"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"后台回复666,领取私人整理的1000道互联网面试题"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章