Tomcat Thread Model – Does a thread in Thread per request model handle all work related to that request?

huangapple go评论86阅读模式
英文:

Tomcat Thread Model - Does a thread in Thread per request model handle all work related to that request?

问题

我理解Servlet容器将使用“每个请求一个线程”的模型,但我的问题是,处理请求的线程是否会执行以下所有步骤?

  1. 从池中获取线程来处理请求,并将HTTP请求和HTTP响应对象传递给Servlet服务方法。
  2. 调用可能涉及延迟的服务/数据访问对象/逻辑,因为数据库中进行了I/O操作。
  3. 返回HTTP响应。
  4. 将线程返回给容器线程池。

我的主要问题是,如果步骤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 ?

  1. Obtain thread from pool to handle request and and pass http request and http response objects to Servlet service method.
  2. Invoke service/dao/ logic which could potentially involve delay since I/O operation is done in DB.
  3. Return the Http response
  4. 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.

Head First Servlets & JSP:

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:

  1. Servlet Container creates HttpServletResponse and HttpServletRequest objects;
  2. Container allocates(creates) a new thread for that request and response objects (Important: in order to isolate client-server communication.);
  3. Container then passes those request and response objects to the servlet thread;
  4. 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.);
  5. 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);
  6. 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.

Head First Servlets & JSP:

> 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:

  1. Servlet Container creates HttpServletResponse and HttpServletRequest objects;
  2. Container allocates(creates) a new thread for that request and response objects (Important: in order to isolate client-server communication.);
  3. Container then passes those request and response objects to the servlet thread;
  4. 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.);
  5. 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);
  6. 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.

huangapple
  • 本文由 发表于 2020年9月27日 06:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定