Tomcat-maxThreads 与 maxConnections

在 Tomcat 的 server.xml中,maxThreadsmaxConnections的比值是多少?

我知道 maxConnections是打开到服务器的连接数。

maxThreads是请求处理线程的最大数量。

但是这两个配置参数是如何协同工作的呢?--显然,您不会将 maxConnections设置为1000,而将 maxThreads设置为10。

这两个配置参数之间的关系是什么?

<Connector
port="8443"
protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="250"
SSLEnabled="true"
scheme="https" secure="true"
clientAuth="false"
sslProtocol="TLS"
connectiontimeout="20000"
/>
148090 次浏览

From Tomcat documentation, For blocking I/O (BIO), the default value of maxConnections is the value of maxThreads unless Executor (thread pool) is used in which case, the value of 'maxThreads' from Executor will be used instead. For Non-blocking IO, it doesn't seem to be dependent on maxThreads.

Tomcat can work in 2 modes:

  • BIO – blocking I/O (one thread per connection)
  • NIOnon-blocking I/O (many more connections than threads)

Tomcat 7 is BIO by default, although consensus seems to be "don't use BIO because NIO is better in every way". (And BIO has been completely thrown out of 8.5.0 and later versions.) You set this using the protocol parameter in the server.xml file.

  • BIO will be HTTP/1.1 or org.apache.coyote.http11.Http11Protocol
  • NIO will be org.apache.coyote.http11.Http11NioProtocol

If you're using BIO then I believe they should be more or less the same.

If you're using NIO then actually "maxConnections=1000" and "maxThreads=10" might even be reasonable. The defaults are maxConnections=10,000 and maxThreads=200. With NIO, each thread can serve any number of connections, switching back and forth but retaining the connection so you don't need to do all the usual handshaking which is especially time-consuming with HTTPS but even an issue with HTTP. You can adjust the "keepAlive" parameter to keep connections around for longer and this should speed everything up.