最近一直在解决线上一个问题,表现是:

Tomcat每到凌晨会有一个高峰,峰值的并发达到了3000以上,最后的结果是Tomcat线程池满了,日志看很多请求超过了1s。

服务器性能很好,Tomcat版本是7.0.54,配置如下:

 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="3000" minSpareThreads="800"/><Connector executor="tomcatThreadPool" port="8084" protocol="org.apache.coyote.http11.Http11AprProtocol"connectionTimeout="60000"keepAliveTimeout="30000"maxKeepAliveRequests="8000"maxHttpHeaderSize="8192"URIEncoding="UTF-8"enableLookups="false"acceptCount="1000"disableUploadTimeout="true"redirectPort="8443" />

事后thread dump看其实真正处于RUNNABLE状态的线程很少,绝大部分线程都处于TIMED_WAITING状态:

于是大伙都开始纠结为什么线程会涨到3000,而且发现即使峰值过了线程数并不会降下来。

我们首先想到的是:

后端应用的处理瞬间比较慢,“堵住了”导致前端线程数涨了起来。

但是优化一个版本上线后发现虽然涨的情况有所好转,但是最终线程池还是会达到3000这个最大值。

==================================分割线=========================================

以上是大背景,中间的过程省略,直接跟各位说下目前我得到的结论:

1、首先是为什么线程不释放的问题?

简单说下我验证的Tomcat(7.0.54)线程池大概的工作机制

  • Tomcat启动时如果没有请求过来,那么线程数(都是指线程池的)为0;
  • 一旦有请求,Tomcat会初始化minSapreThreads设置的线程数;
  • Tomcat不会主动对线程池进行收缩,除非确定没有任何请求的时候,Tomcat才会将线程池收缩到minSpareThreads设置的大小;
  • Tomcat6之前的版本有一个maxSpareThreads参数,但是在7中已经移除了,所以只要前面哪怕只有一个请求,Tomcat也不会释放多于空闲的线程。

至于Tomcat为什么移除maxSpareThreads这个参数,我想也是出于性能的考虑,不停的收缩线程池性能肯定不高,而多余的线程处于等待状态的好处是一有新请求过来立刻可以处理。

  • 而且大量的Tomcat线程处于等待状态不会消耗CPU,但是会消耗一些JVM存储。

补充:上面标红的一句有点问题,进一步验证发现只有使用Keep-Alive(客户端和服务端都支持)时才是这种表现,如果客户端没有使用Keep-Alive那么线程会随着TCP连接的释放而回收。

Tomcat中Keep-Alive相关的参数:

maxKeepAliveRequests:

The 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.

keepAliveTimeout:

The number of milliseconds this Connector will wait for another HTTP request before closing the connection. The default value is to use the value that has been set for the connectionTimeout attribute. Use a value of -1 to indicate no (i.e. infinite) timeout.

2、为什么线程池会满?

这是我现在纠结的核心。到底是不是应用的性能慢导致的,我现在的结论是有关系,但关键是并发。

  • Tomcat的线程池的线程数跟你的瞬间并发有关系,比如maxThreads设置为1000,当瞬间并发达到1000那么Tomcat就会起1000个线程来处理,这时候跟你应用的快慢关系不大。

那么是不是并发多少Tomcat就会起多少个线程呢?这里还跟Tomcat的这几个参数设置有关系,看官方的解释是最靠谱的:

maxThreads:

The 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.

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 the default is 10000. For APR/native, the default is 8192.

……

acceptCount:

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.

minSpareThreads:

The minimum number of threads always kept running. If not specified, the default of 10 is used.

我简单理解就是:

maxThreads:Tomcat线程池最多能起的线程数;

maxConnections:Tomcat最多能并发处理的请求(连接);

acceptCount:Tomcat维护最大的对列数;

minSpareThreads:Tomcat初始化的线程池大小或者说Tomcat线程池最少会有这么多线程。

比较容易弄混的是maxThreads和maxConnections这两个参数:

maxThreads是指Tomcat线程池做多能起的线程数,而maxConnections则是Tomcat一瞬间做多能够处理的并发连接数。比如maxThreads=1000,maxConnections=800,假设某一瞬间的并发时1000,那么最终Tomcat的线程数将会是800,即同时处理800个请求,剩余200进入队列“排队”,如果acceptCount=100,那么有100个请求会被拒掉。
注意:根据前面所说,只是并发那一瞬间Tomcat会起800个线程处理请求,但是稳定后,某一瞬间可能只有很少的线程处于RUNNABLE状态,大部分线程是TIMED_WAITING,如果你的应用处理时间够快的话。所以真正决定Tomcat最大可能达到的线程数是maxConnections这个参数和并发数,当并发数超过这个参数则请求会排队,这时响应的快慢就看你的程序性能了。

以上的结论都是我个人验证和总结,如有不对,跪求指正!!!

聊下并发和Tomcat线程数(Updated)相关推荐

  1. tomcat线程释放时间_聊下并发和Tomcat线程数(错误更正)

    本文前半部分结论存在严重错误,请看最后2015-1-20更新部分. 最近一直在解决线上一个问题,表现是: Tomcat每到凌晨会有一个高峰,峰值的并发达到了3000以上,最后的结果是Tomcat线程池 ...

  2. 聊下并发和Tomcat线程数(错误更正)

    本文前半部分结论存在严重错误,请看最后2015-1-20更新部分. 最近一直在解决线上一个问题,表现是: Tomcat每到凌晨会有一个高峰,峰值的并发达到了3000以上,最后的结果是Tomcat线程池 ...

  3. tomcat优化——并发和Tomcat线程数

    最近一直在解决线上一个问题,表现是: Tomcat每到凌晨会有一个高峰,峰值的并发达到了3000以上,最后的结果是Tomcat线程池满了,日志看很多请求超过了1s. 服务器性能很好,Tomcat版本是 ...

  4. windows查看tomcat连接数_Springboot内置Tomcat线程数优化

    # 等待队列长度,默认100.队列也做缓冲池用,但也不能无限长,不但消耗内存,而且出队入队也消耗CPU server.tomcat.accept-count=1000 # 最大工作线程数,默认200. ...

  5. tomcat如何增大并发_tomcat最大线程数,单台tomcat最大并发量

    tomcat最大线程数,单台tomcat最大并发量,tomcat最大线程数的设置 Connector port="8080" maxThreads="150"  ...

  6. SpringBoot 内置 Tomcat 线程数优化配置,你学会了吗?

    前言 本文解析springboot内置tomcat调优并发线程数的一些参数,并结合源码进行分析 参数 线程池核心线程数 server.tomcat.min-spare-threads:该参数为tomc ...

  7. 【线程池的工作参数、什么情况下会触发最大线程数?什么情况下会回收线程?】

    线程池是一个重要的 Java 并发编程工具,用于控制线程的创建.调度和回收,可以有效地提高程序的性能和资源利用率. 线程池的工作参数包括以下几个方面: 核心线程数(corePoolSize): 表示线 ...

  8. 性能测试:深入理解并发量,线程数,吞吐量,TPS

    并发数,线程数,吞吐量,每秒事务数(TPS)都是性能测试领域非常关键的数据和指标. 那么他们之间究竟是怎样的一个对应关系和内在联系? 测试时,我们经常容易将线程数等同于表述为并发数,这一表述正确吗? ...

  9. linux下查看进程的线程数,linux查看进程的线程数

    top -H -p $PID  #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...

最新文章

  1. oracle存储过程循环单列,Oracle存储过程循环语法实例分析
  2. linux程序打包安装,制作Linux下程序安裝包——使用腳本打包bin、run等安裝包
  3. Windows Server 2008 NFS
  4. nosql----redis持久化详解
  5. iPhone 12 Mini曝光:售价5000内、电池容量不忍看
  6. idea json格式化插件_IDEA常用插件
  7. CIA 数据泄露事件报告出炉:光为他人做嫁衣,自家着火不自知
  8. jQuery中的map()方法
  9. [ASP.NET]UserControl對UserControl參數傳遞
  10. 网页设计(二)——HTML与BOX
  11. 还在用PS?程序猿:给我200行Python代码分分钟给你批量换脸
  12. 2019最新财务管理计算器源码
  13. linux 直流电机驱动设计,嵌入式Linux直流电机驱动.PPT
  14. 非结构数据分析与建模——垃圾短信数据集
  15. 【每日蓝桥】44、一七年省赛Java组真题“纸牌三角形”
  16. ThreadLocal使用时因线程复用导致数据混乱分析
  17. [VB.net]绘制具有渐变颜色和防锯齿字体的标题
  18. [翻译/转载] 部分使用intel RST硬盘驱动与电脑安装Linux系列系统相互冲突 导致无法装linux系统的解决方法
  19. Android开发-在Android应用里嵌入腾讯广告联盟进行盈利实现
  20. Jetson Nano、TX2等 conda 虚拟环境中使用TensorRT、gi等

热门文章

  1. 让电影动漫统统变丝滑,480帧也毫无卡顿,交大博士生开源插帧软件DAIN
  2. 滴滴AI负责人叶杰平:你的每一次出行,都已有AI落地的助力 | MEET 2020
  3. 百度Apollo升级发布15大新品,还要化身无人车基建狂魔 | 一文看尽首届Apollo生态大会...
  4. 地平线新征程!官网焕然一新,二代自动驾驶芯片势已蓄,只待发
  5. 英伟达“暴力碾压”谷歌:53分钟训练完BERT,2.2毫秒完成推理,创下NLP三项新纪录...
  6. Squid配置二级代理(父代理)
  7. BC #49 1001 Untitled
  8. jsp 4种常用语法3个编译指令7个动作指令
  9. php 字符串转数组 提取中文 提取英文 字符串类型
  10. 为什么工业控制系统需要安全防护?