Diego Ramos
Diego Ramos

Reputation: 1059

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

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.

Upvotes: 4

Views: 1774

Answers (1)

Giorgi Tsiklauri
Giorgi Tsiklauri

Reputation: 11110

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.

Upvotes: 1

Related Questions