英文:
Tomcat Thread Model - Does a thread in Thread per request model handle all work related to that request?
问题
我理解Servlet容器将使用“每个请求一个线程”的模型,但我的问题是,处理请求的线程是否会执行以下所有步骤?
- 从池中获取线程来处理请求,并将HTTP请求和HTTP响应对象传递给Servlet服务方法。
- 调用可能涉及延迟的服务/数据访问对象/逻辑,因为数据库中进行了I/O操作。
- 返回HTTP响应。
- 将线程返回给容器线程池。
我的主要问题是,如果步骤2中的I/O操作花费了大量时间,Servlet容器是否会耗尽池中的线程?还是容器仅使用一个/多个线程来处理请求,然后将工作委托给另一个线程来完成其余的工作?另外,我听说现在他们正在将模型更改为带有NIO操作的线程模型?谢谢。
英文:
I understand that the Servlet Containers will use "Thread per request" model, but my question is, will the thread handling the request do all the below steps ?
- Obtain thread from pool to handle request and and pass http request and http response objects to Servlet service method.
- Invoke service/dao/ logic which could potentially involve delay since I/O operation is done in DB.
- Return the Http response
- Return the thread to the Container Thread pool
My main questions is, if somehow the I/O operation on step 2 takes a huge amount of time, will the Servlet container run out of threads from the pool ? Or does the Container use one thread/threads just to handle the request and then delegates the work to another thread to do the rest of the work ? Also I heard that nowadays they are changing the model to a Threaded Model with NIO operations? Thank you.
答案1
得分: 1
will the same thread be used for everything ?
TL;DR - No.
Once the Servlet Container (Catalina) spins up the thread per request, that thread is deallocated/exited right after that request-response cycle is finished (that is, corresponding HTTP request handler Servlet method returns).
If your service (DAO/logic/whatever) layer will block the thread, which eventually blocks the web layer (
doGet()
,doPost()
or etc.), browser will go idle, awaiting the response (time is either default or configured), and Catalina (Servlet Container) will block that thread only (other requests may arrive successfully);I/O (or to be specific Request-Response) timeout will be either default (which is 60 seconds, but it depends on the Tomcat version), or configured by yourself;
Design of the architecture, to delegate discrete incoming HTTP Message to separate threads, has a sole and simple purpose - to process the Request-Response cycles in isolation.
The Container automatically creates a new Java thread for every servlet request it receives. When the servlet’s done running the HTTP service method for that client’s request, the thread completes (i.e. dies).
Update to your updated question
my question is, will the thread handling the request do all the below steps?
TL;DR - No again.
Servlet Objects live in container, which is a completely separate thread.
When the HTTP message (request, in this case) hits the Servlet-mapped endpoint, this happens:
- Servlet Container creates
HttpServletResponse
andHttpServletRequest
objects;- Container allocates(creates) a new thread for that request and response objects (Important: in order to isolate client-server communication.);
- Container then passes those request and response objects to the servlet thread;
- Container then calls the Servlet API's
service()
method and depending on what is the type of incoming message (GET
,POST
, etc.), it invokes corresponding method (doGet();
doPost();
etc.);- Container DOES NOT CARE whatever levels or layers of architecture you have - DAO, Repository, Service, Cherry, Apple or whatever. It will wait until the corresponding HTTP request handler method finishes (accordingly, if something blocks it, container will block that thread);
- When the handler method returns; thread is deallocated.
Answering your further questions
My main questions is, if somehow the I/O operation on step 2 takes a huge amount of time, will the Servlet container run out of threads from the pool ?
Theoretically it can; however, that means, that it should block all the 200 threads at the same time and this time (if the default configuration is maintained) it will not accept any other requests (until some thread deallocates).
This, however, can be configured with
maxThreads
attribute and you can choose what should be the threshold number of request processing threads allowed in Tomcat.Or does the Container use one thread/threads just to handle the request and then delegates the work to another thread to do the rest of the work?
We have answered this above.
Also I heard that nowadays they are changing the model to a Threaded Model with NIO operations?
NIO specific configuration and it can facilitate poller threads, which are used to simultaneously handle multiple connections per thread; however, this is a big and completely different topic. For the further reading, have a look a this and this.
PLEASE, make sure that your future posts are not too broad, containing 10 different questions in a single post.
英文:
> will the same thread be used for everything ?
TL;DR - No.
Once the Servlet Container (Catalina) spins up the thread per request, that thread is deallocated/exited right after that request-response cycle is finished (that is, corresponding HTTP request handler Servlet method returns).
If your service (DAO/logic/whatever) layer will block the thread, which eventually blocks the web layer (doGet()
, doPost()
or etc.), browser will go idle, awaiting the response (time is either default or configured), and Catalina (Servlet Container) will block that thread only (other requests may arrive successfully);
I/O (or to be specific Request-Response) timeout will be either default (which is 60 seconds, but it depends on the Tomcat version), or configured by yourself;
Design of the architecture, to delegate discrete incoming HTTP Message to separate threads, has a sole and simple purpose - to process the Request-Response cycles in isolation.
> The Container automatically creates a new Java thread for every servlet request it receives. When the servlet’s done running the HTTP service method for that client’s request, the thread completes (i.e. dies).
Update to your updated question
>my question is, will the thread handling the request do all the below steps?
TL;DR - No again.
Servlet Objects live in container, which is a completely separate thread.
When the HTTP message (request, in this case) hits the Servlet-mapped endpoint, this happens:
- Servlet Container creates
HttpServletResponse
andHttpServletRequest
objects; - Container allocates(creates) a new thread for that request and response objects (Important: in order to isolate client-server communication.);
- Container then passes those request and response objects to the servlet thread;
- Container then calls the Servlet API's
service()
method and depending on what is the type of incoming message (GET
,POST
, etc.), it invokes corresponding method (doGet();
doPost();
etc.); - Container DOES NOT CARE whatever levels or layers of architecture you have - DAO, Repository, Service, Cherry, Apple or whatever. It will wait until the corresponding HTTP request handler method finishes (accordingly, if something blocks it, container will block that thread);
- When the handler method returns; thread is deallocated.
Answering your further questions
> My main questions is, if somehow the I/O operation on step 2 takes a huge amount of time, will the Servlet container run out of threads from the pool ?
Theoretically it can; however, that means, that it should block all the 200 threads at the same time and this time (if the default configuration is maintained) it will not accept any other requests (until some thread deallocates).
This, however, can be configured with maxThreads
attribute and you can choose what should be the threshold number of request processing threads allowed in Tomcat.
> Or does the Container use one thread/threads just to handle the request and then delegates the work to another thread to do the rest of the work?
We have answered this above.
> Also I heard that nowadays they are changing the model to a Threaded Model with NIO operations?
NIO specific configuration and it can facilitate poller threads, which are used to simultaneously handle multiple connections per thread; however, this is a big and completely different topic. For the further reading, have a look a this and this.
PLEASE, make sure that your future posts are not too broad, containing 10 different questions in a single post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论