假设 Tomcat 最多支持 5 个线程,并且有 5 个线程正在进行中(假设这些请求将花费大量时间)。现在 1 个请求创建了另外 2 个执行某些操作的线程,
- 那么这 2 个线程将获得 CPU 还是将等待?
- 如果他们等待,他们会在操作系统队列中等待吗? (接受连接队列)
最佳答案
这是the documentation for maxThreads :
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.
线程限制是为 Tomcat 用于分配传入请求的内部池配置的最大数量。这并不是 JVM 使用的线程数的硬性限制。
如果您的请求在工作过程中创建了更多线程(调用new Thread()
),则这些线程不是来自池,而是从操作系统请求的。
但最好的方法可能是使用单独的专用线程池。您始终可以配置自己的线程池,并让您的请求从该池中获取线程。这样,您就不会在线程耗尽的情况下运行系统(如果出现失控并且线程开始挂起,您可能会冒创建新线程的风险),并且不会降低 Tomcat 服务请求的能力(如果您使用 Tomcat 的池,则会这样做)。
关于java - 如果任何请求在 tomcat 线程池中创建更多线程怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53380194/