Tomcat中的线程配置

来源:互联网 发布:readcube for mac 编辑:程序博客网 时间:2024/06/06 03:07

一、概述

    tomcat中在server.xml中配置线程,可以在connector中配置,也可以配置executor,这两者之间的配置有什么区别?

    具体配置格式及内容,还请查看conf/server.xml配置文件,默认是注释掉了。


二、详解

1. 概述

    线程池和连接器的关系,在连接器配置选项中清晰的被陈述出来。

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.


    连接器中的线程配置是私有的,连接器自己的配置只能自己使用;而线程池可以被共享,多个连接器通过executor属性关联到连接池的name完成配置(见下图);一旦连接器中配置了一个存在的executor,那么线程池的配置将会覆盖连接器的线程配置。


The Executor represents a thread pool that can be shared between components in Tomcat. Historically there has been a thread pool per connector created but this allows you to share a thread pool, between (primarily) connector but also other components when those get configured to support executors
Each incoming request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCount attribute). Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them.

    线程池覆盖连接器中的线程配置,主要是覆盖maxThreads和minSpareThreads两个参数,这两个参数在线程池标签和连接器标签中都存在。

    a. 对于线程池而言:最主要的配置项是maxThreads和minSpareThreads(参看3.1表格,官网全部配置项参看4.1链接),前者代表线程池中可以创建的最大线程数,后者代表当线程池空闲的时候保留的线程数量,当线程池覆盖连接器的线程配置的时候,最主要的是覆盖这两个参数,连接器如何关联线程池,参看上文截图。

    b. 对于连接器而言:线程相关的主要参数有四个——maxThreads、minSpareThreads、maxConnections、acceptCount,如果连接器关联了线程池,那么maxThreads和minSpareThreads会被覆盖(重要的事情反复说),acceptCount和maxConnections继续生效;

    maxThreads:一个请求分配一个线程处理,其实就是serversocket.accept之后,新建了一个线程去处理这个接受的socket,然后这个连接器可以建立的最大线程数量即为maxThreads配置的值,如果是多个连接器关联了同一个线程池,那么多个连接器允许建立的线程数量之和即为maxThreads的值;

    minSpareThreads:如果请求少或者没有请求,那么tomcat就会主动关闭一些不用的线程,但是至少会保留minSpareThreads配置的线程数;

    maxConnections:表示可以同时被当前连接器处理的请求数量,这个配置项需要和maxThreads进行对比,一个请求一旦被接受处理就会分配给一个线程,这时候能够同时处理多少个请求取决于maxConnections和maxThreads两个配置项中的较小者,如果连接器是BIO则maxConnections默认等于maxThreads(连接器的maxThreads,如果关联了线程池,则为线程池中的maxThreads),NIO2默认是10000,APR默认是8192(BIO/NIO/NIO2/APR主要是根据connector的protocol属性,这个属性可以配置小表中的类);

    举个例子,连接器中maxThreads=200,maxConnections=100,这时候因为线程池最多只允许创建100个线程,因此能被同时处理的请求数量是100,取决于两者中的较小者;但是如果连接器关联了线程池maxThreads=200,连接器A的maxConnections=120,连接器B的maxConnections=100,这时候对于连接器A而言,处理请求的最大的线程数是120,不会超过120,但是能不能达到120还需要看线程池中的可用的线程数,如果这时候连接器B已经拿了100个线程,那么此时A只能拿到100个线程;


Java Blocking Connector
BIO
Java Nio Connector
NIO
Java Nio2 Connector
NIO2
APR/native Connector
APR
ClassnameHttp11ProtocolHttp11NioProtocolHttp11Nio2ProtocolHttp11AprProtocol

    acceptCount:相当于建立ServerSocket的时候构造函数中传入的backlog参数,表示serversocket中完成三次握手后(成功建立TCP连接)允许等待被accept的socket数量,已经建立好了TCP连接,但是socket还没有被accept取出来处理;


2. 小结

    tomcat中能够同时存在并被处理的请求数是maxThreads和maxConnections中的较小者(不管是连接器私有配置还是关联一个线程池);当正在被处理的请求数量达到这个值的时候,再过来的请求会被接受(TCP连接建立成功),但是没有被处理,被阻塞到serversocket上(对客户端浏览器而言,就是响应慢,页面标签一直在刷新等待状态,直到请求被处理),接受并被阻塞的socket请求数的最大值为acceptCount;当阻塞达到acceptCount的最大数目时,在发送过来的请求会建立连接refuse,对客户端而言,返回连接被拒绝的错误。

    所以,tomcat能接受的最大请求数量为maxThreads or maxConnections中的较小者,加上acceptCount的数量,其中前者中的较小者是正在被处理的请求数量,acceptCount为等待被处理的请求数量,超过这两者之和的请求会被拒绝。


三、官网相关配置详解

1. 线程池的主要配置

AttributeDescriptionmaxThreads

(int) The max number of active threads in this pool, default is 200

minSpareThreads

(int) The minimum number of threads always kept alive, default is 25

maxIdleTime

(int) The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

maxQueueSize

(int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is Integer.MAX_VALUE


2. 连接器的主要配置

AttributeDescriptionacceptCount

The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

executorA 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.maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO and NIO2 the default is 10000. For APR/native, the default is 8192.

Note that for APR/native on Windows, the configured value will be reduced to the highest multiple of 1024 that is less than or equal to maxConnections. This is done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connections are not counted.

maxKeepAliveRequestsThe maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.maxThreadsThe maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.minSpareThreadsThe minimum number of threads always kept running. If not specified, the default of 10 is used. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.


四、官网相关资源链接

    1. executor:http://tomcat.apache.org/tomcat-8.0-doc/config/executor.html

    2. connector:http://tomcat.apache.org/tomcat-8.0-doc/config/http.html


附注:

  本文如有错漏,烦请不吝指正,谢谢!

0 0
原创粉丝点击