Tomcat中的线程配置

一、概述

    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配置的线程数;

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值