读书人

tomcat源码学习-线程池

发布时间: 2012-07-08 17:43:43 作者: rapoo

tomcat源码学习--线程池
写个前言:
通常大家知道tomcat的线程池是可以配置的,去找到conf下面的的server.xml就可以搞定,先配置指定参数
maxThreads="150" minSpareThreads="4"/>
-->

在配置连接器使用它,注释中都给出了例子
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->


但现在不是想说这个,是想看看tomcat源码里的线程池,只看配置文件没啥意义,不好玩
开始->>
首先看tomcat的源码,ThreadPool里面的成员变量



初始化一切搞定,当线程调用run,那么会去查找相关已经初始化的线程,再取出来,对应去运行
/** Implemented if you want to run a piece of code inside a thread pool. */public interface ThreadPoolRunnable {    // XXX use notes or a hashtable-like    // Important: ThreadData in JDK1.2 is implemented as a Hashtable( Thread -> object ),    // expensive.        /** Called when this object is first loaded in the thread pool.     *  Important: all workers in a pool must be of the same type,     *  otherwise the mechanism becomes more complex.     */    public Object[] getInitData();    /** This method will be executed in one of the pool's threads. The     *  thread will be returned to the pool.     */    public void runIt(Object thData[]);}

那么tomcat又在何处使用它了,利用eclipse工具,找到调用类PoolTcpEndpoint,里面的线程是LeaderFollowerWorkerThread还实现了ThreadPoolRunnable,感兴趣可以自己去看看

综上所述,它实现是怎样的,先初始化一定数量的线程,他们都先睡着,线程池 runIt(listener)传入线程,传入后就不notify然后就 shouldRun = true,然后就调用传进来的线程。但是包装的东西好多啊,控制的东西验证的东西也好多,造成了这个类好大。





读书人网 >行业软件

热点推荐