Java Tomcat 的线程池究竟是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22854498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:24:31  来源:igfitidea点击:

How exactly does Tomcat's Threadpool work

javamultithreadingtomcatthreadpool

提问by Kraken

So here's my understanding.

所以这是我的理解。

The Apache Tomcat's threadpool (is it called the connector Threadpool? ) has a number of threads ( 200 by default ). Now this means that at a particular time, 200 people can communicate with my web application.

Apache Tomcat 的线程池(是否称为连接器线程池?)有许多线程(默认为 200)。现在这意味着在特定时间,有 200 人可以与我的 Web 应用程序进行通信。

Now, taking a scenario when a particular connects with my application. I get a request, and the tomcat client on the server checks if there is any thread available to cater to the request or not. If it is then good, else we give back some error code. Now in case of success, will that one thread, that I have assigned to that request, be associated with that particular client till the time his request is not handled?

现在,当一个特定的连接到我的应用程序时,假设一个场景。我收到一个请求,服务器上的 tomcat 客户端检查是否有任何线程可以满足该请求。如果它是好的,否则我们返回一些错误代码。现在,如果成功,我分配给该请求的那个线程是否会与该特定客户端相关联,直到他的请求未被处理?

Basically, as a user if I go to, www.myApp.com, I get one thread from the thread pool that fetches all the info from the server and gives me back the final html. After that the thread is free and goes back to pool. Now if I click on something else then again go to the threadpool and get assigned a thread if available and it goes on a on. Is this how it works?

基本上,作为用户,如果我去www.myApp.com,我会从线程池中获取一个线程,该线程从服务器获取所有信息并将最终的 html 返回给我。之后线程空闲并返回到池中。现在,如果我单击其他内容,然后再次转到线程池,并在可用时分配一个线程,然后继续运行。这是它的工作原理吗?

Question 2

问题2

Let's say, I have a Java Application on my server side. So when I hit a url, it takes me to some java code. Now is it that, the thread that I got from Tomcat Threadpool, that thread will execute my code?

假设我的服务器端有一个 Java 应用程序。所以当我点击一个 url 时,它会带我到一些 java 代码。现在是不是,我从 Tomcat 线程池获得的线程,该线程将执行我的代码?

If that is so, what happens if I make new threads in my java code, will those threads be taken from the Tomcat's threadpool or will those be from the Java's threadpool created? Also, what happens to the execution of the Tomcat's thread in that case?

如果是这样,如果我在我的 java 代码中创建新线程会发生什么,这些线程是从 Tomcat 的线程池中获取的,还是从创建的 Java 线程池中获取的?另外,在这种情况下,Tomcat 线程的执行会发生什么?

Thanks.

谢谢。

Kindly provide some Tomcat official references/resources where I can read about such things if you know about any. Cheers.

请提供一些 Tomcat 官方参考资料/资源,如果您知道的话,我可以在其中阅读此类内容。干杯。

采纳答案by Stephen C

Now this means that at a particular time, 200 people can communicate with my web application.

现在这意味着在特定时间,有 200 人可以与我的 Web 应用程序进行通信。

Not exactly. It means that your server can process200 requests simultaneously1. There can be a number of other requests in the queue waiting for a thread to become available.

不完全是。这意味着您的服务器可以同时处理200 个请求1。队列中可能有许多其他请求在等待线程变为可用。

Now in case of success, will that one thread, that I have assigned to that request, be associated with that particular client till the time his request is not handled?

现在,如果成功,我分配给该请求的那个线程是否会与该特定客户端相关联,直到他的请求未被处理?

Yes ... unless you are using the asynchronous request handling features added in a recent version of the Servlet spec. (In that case, it may be possible to process more than 200 requests "simultaneously" with 200 threads. But that would entail one request surrendering control to another request while it waits for something to happen. Read thisfor introduction.)

是的……除非您使用的是最近版本的 Servlet 规范中添加的异步请求处理功能。(在这种情况下,有可能用 200 个线程“同时”处理 200 多个请求。但这将需要一个请求在等待某事发生时将控制权交给另一个请求。阅读本文以了解介绍。)

Is this how it works?

这是它的工作原理吗?

More or less ...

或多或少 ...

1 - To be pedantic, you would (probably) need 200+ cores for the processing of 200 requests to happen at the same instant in time. So, I'm using "simultaneously" from the perspective of the end-users, who have no visibility of what is actually happening inside the server "black box". But having said that, it is not impossible for one physical thread / core to be performing work for multiple requests at the same instant in time. The most obvious case is where there are lots of identical requests that are handled together.

1 - 为了学究,您(可能)需要 200 多个内核才能同时处理 200 个请求。因此,我从最终用户的角度“同时”使用,他们无法看到服务器“黑匣子”内部实际发生的情况。但话虽如此,一个物理线程/核心在同一时刻为多个请求执行工作并非不可能。最明显的情况是有很多相同的请求被一起处理。



Let's say, I have a Java Application on my server side. So when I hit a url, it takes me to some java code. Now is it that, the thread that I got from Tomcat Threadpool, that thread will execute my code?

假设我的服务器端有一个 Java 应用程序。所以当我点击一个 url 时,它会带我到一些 java 代码。现在是不是,我从 Tomcat 线程池获得的线程,该线程将执行我的代码?

This doesn't make sense. If you have a Java application on the server side, then you need to explain howyou go from an HTTP request ("hitting[sic] a url") to running the Java application. Only then can we tell you whether or not a threadpool thread is involved.

这没有意义。如果您在服务器端有 Java 应用程序,那么您需要解释如何从 HTTP 请求(“hitting[sic] a url”)到运行 Java 应用程序。只有这样我们才能告诉您是否涉及线程池线程。

If that is so, what happens if I make new threads in my java code, will those threads be taken from the Tomcat's threadpool or will those be from the Java's threadpool created? Also, what happens to the execution of the Tomcat's thread in that case?

如果是这样,如果我在我的 java 代码中创建新线程会发生什么,这些线程是从 Tomcat 的线程池中获取的,还是从创建的 Java 线程池中获取的?另外,在这种情况下,Tomcat 线程的执行会发生什么?

Once again, it depends how your Java application is being run. For instance, if your webapp is using Runtime.exec("java ... classname")to run the application on the server-side, then it is in a separate JVM to your Tomcat, and none of the application threads will be in the Tomcat thread pool.

同样,这取决于您的 Java 应用程序的运行方式。例如,如果您的 webRuntime.exec("java ... classname")应用程序用于在服务器端运行应用程序,那么它位于与 Tomcat 不同的 JVM 中,并且没有任何应用程序线程将位于 Tomcat 线程池中。

It has also been pointed out that there is no "Java threadpool" per se. If your Java application chooses to, it can create and use a thread pool. But if it doesn't, then Java threads are not pooled. They are largely disposed of when they terminate, and any remaining data structures are reclaimed when the Threadobject is garbage collected.

也有人指出,本身不存在“Java 线程池” 。如果您的 Java 应用程序选择这样做,它可以创建和使用线程池。但如果不是,则 Java 线程不会被合并。它们在终止时大部分被处理掉,并且当Thread对象被垃圾收集时,任何剩余的数据结构都会被回收。