8

空中楼阁之纸上谈兵 线程池执行流程图

 3 years ago
source link: https://club.perfma.com/article/2390341
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
文章>空中楼阁之纸上谈兵 线程池执行流程图

空中楼阁之纸上谈兵 线程池执行流程图

青叶竹1周前

一、jdk中的线程池

线程池java.util.concurrent.ThreadPoolExecutor中execute方法执行流程图如下
image.png
image.png


如图所示,要想扩展线程池,任务队列是否已满部分可以进行重写,即扩展BlockQueue,如tomcat、dubbo中都是自己新建了TaskQueue,其继承自LinkedBlockQueue,并重写了offer方法。
另需要继承java.util.concurrent.ThreadPoolExecutor,并重写execute方法,并扩展afterExecute方法。


备注:
流程图中的线程池假设线程池的状态是运行状态
另线程池状态变化如下图所示
image.png


1、tomcat下的线程池

tomcat中的ThreadPoolExecutor的execute方法如下
image.png

tomcat的TaskQueue中重写部分的执行流程图如下
image.png
image.png


2、dubbo下的线程池

dubbo的EagerThreadPoolExecutor的execute方法如下
image.png

dubbo的TaskQueue中重写部分的执行流程图如下
image.png
image.png


总结:
1、首先要明确,能执行到添加任务到队列中,当前的线程数必然达到了核心线程数;
2、其次,线程池的目的是为了快速执行任务为目标,那么当任务有积压时,这时最好能将线程数提高到最大,这样资源利用率才能最大化;
3、综上所述:先利用已经创建好的线程,执行刚添加的任务;
其次,为了加快执行效率,避免任务积压过多,先将线程数扩展到最大;
最后,再将任务进行积压。


欢迎各位批评指正,共同学习进步^_^😊

0人觉得很赞
1014 阅读
请先登录,再评论

暂无回复,快来写下第一个回复吧~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK