简体   繁体   English

应用程序级线程池和tomcat

[英]application level thread pool and tomcat

Let's say a java application has it's own thread pool to support concurrent task execution and a total of 500 threads are allocated to it. 假设Java应用程序拥有自己的线程池来支持并发任务执行,并且总共分配了500个线程。 Tomcat is also configured to support an overall 1000 threads via the connector configuration. Tomcat还配置为通过连接器配置支持总共1000个线程。

Does the connector configuration for 1000 threads act as a superset for the 500 threads application thread pool or are these two independent thread pools? 是1000个线程的连接器配置充当500个线程应用程序线程池的超集还是这两个独立的线程池? Does this mean a total of 1000 threads are allocated to the deployed application or is it 500 + 1000 = 1500? 这是否意味着总共1000个线程分配给已部署的应用程序,还是500 + 1000 = 1500?

When you configure the connector to have max 1000 threads, the connector will have 1000 threads max. 当您配置连接器最多具有1000个线程时,连接器最多具有1000个线程。 It doesn't check how many threads might be active within the same VM. 它不会检查同一VM中可能有多少个活动线程。

You don't state how you allocate your custom thread pool, but I've never come across a thread pool that is relating the number of its allowed threads to what else is going on in the system (other than running out of memory). 您没有说明如何分配自定义线程池,但是我从未遇到过将其允许的线程数与系统中正在发生的事情(而不是内存不足)相关联的线程池。

The reason to your answer would be 1500 then. 回答的原因是1500。

It's also easy to find out: Just put your system under the load that it's supposed to handle with 1000 threads and cause a thread dump. 这也很容易发现:只需将您的系统置于本应处理的1000个线程的负载下,并导致线程转储。 When you configure a connector with 1000 threads, you're clearly doing this because you've measured that this is the appropriate size for the volume that you handle, and that implies that you have load tests that can easily generate that load. 当您配置具有1000个线程的连接器时,您显然正在这样做,因为您已经测量到这是您要处理的卷的适当大小,这意味着您具有可以轻松生成该负载的负载测试。

And rather than spawning all those threads, I'd like to recommend looking into java.util.concurrent.Executor and its relatives. 除了建议生成所有这些线程之外,我还建议您研究java.util.concurrent.Executor及其亲戚。

Java's background thread pool is actually re-used by some parts of the base library. Java的后台线程池实际上被基础库的某些部分重复使用。 So if you for example run a parallelized stream, the exact same thread pool is used for all those streams. 因此,例如,如果您运行并行化的流,则所有这些流都使用完全相同的线程池。

Apache software tends to duplicate the base Java library for reasons beyond me, which means that Tomcat uses its own version of a thread pool that is in no way compatible to or re-using the base library thread pool. Apache软件倾向于出于除我之外的原因来复制基础Java库,这意味着Tomcat使用其自己的线程池版本,该版本绝不兼容或重复使用基础库线程池。

Effectively if you run 500 Java base library thread pool threads and 1000 Tomcat threads, then you are running 1500 threads. 实际上,如果您运行500个Java基库线程池线程和1000个Tomcat线程,那么您正在运行1500个线程。 And if you take into account that thread switching comes with a small cost, having so many active threads will cause the system to spent a noticeable amount of time on thread-handling instead of executing the actual code. 而且,如果考虑到线程切换的成本很小,那么拥有这么多活动线程将导致系统在线程处理上花费大量时间,而不是执行实际代码。 How much exactly completely depends on the internals and the load, but an educated guess for the situation you described would be around 5-15% of CPU time wasted for thread switching. 多少信息完全取决于内部和负载,但是根据您所描述的情况,有根据的猜测可能是线程切换浪费了CPU时间的5-15%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM