测试tomcat线程数:
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="2" minSpareThreads="1" maxQueueSize="1"/>
<Connector port="8080" executor="tomcatThreadPool" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
<Connector executor="tomcatThreadPool" port="8009" protocol="AJP/1.3" redirectPort="8443" />
启用的协议是apr协议。因为我用了executor属性,官方文档中对Connector标签中的executor属性是这么说的:
A reference to the name in an Executor element. If this attribute is set, and the named executor exists, the connector will use the executor, and all the other thread attributes will be ignored. Note that if a shared executor is not specified for a connector then the connector will use a private, internal executor to provide the thread pool. |
maxThreads | (int) The max number of active threads in this pool, default is |
minSpareThreads | (int) The minimum number of threads always kept alive, default is |
maxQueueSize | (int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is |
如果我的executor标签中不设置maxQueneSize属性,那么默认的为int的最大值,意思就是:如果我不设置这个,那么请求数在达到Integer.MAX_VALUE之前是不会被拒绝的,都会放在队列里面等待执行。
如果我按照上面的配置,最小存活数是1,最大活跃线程数是2,队列里面最大是1,那么当我们重启tomcat的时候,第一次去请求会发现一共只有3个请求线程被执行(2+1),剩下的请求都会被tomcat直接reject拒绝掉,如果你去捕获异常,会发现抛出如下错误:
警告: Socket processing request was rejected for:469001312
java.util.concurrent.RejectedExecutionException: Work queue full.
at org.apache.catalina.core.StandardThreadExecutor.execute(StandardThreadExecutor.java:172)
at org.apache.tomcat.util.net.AprEndpoint.processSocketWithOptions(AprEndpoint.java:882)
at org.apache.tomcat.util.net.AprEndpoint$Acceptor.run(AprEndpoint.java:1088)
at java.lang.Thread.run(Thread.java:745)
一月 10, 2017 2:59:34 下午 org.apache.tomcat.util.net.AprEndpoint processSocket
警告: Socket processing request was rejected for:468993104
java.util.concurrent.RejectedExecutionException: Work queue full.
at org.apache.catalina.core.StandardThreadExecutor.execute(StandardThreadExecutor.java:172)
at org.apache.tomcat.util.net.AprEndpoint.processSocket(AprEndpoint.java:913)
at org.apache.tomcat.util.net.AprEndpoint$Poller.run(AprEndpoint.java:1973)
at java.lang.Thread.run(Thread.java:745)
socket请求被拒绝,直接返回502错误。
当我们再次用多线程去请求的时候,发现此时就不一定只有3个线程可用了,会发现基本上都会大于3个?(why)