Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Threads allocated to handle Servlet request?

Can someone please explain what is thread per request and thread per connection? Which model do servlets work on? How threads are allocated to handle HTTP requests? Is it thread/request or connection?

And let's say if I want to perform a time consuming task in my Servlet's doGet() method asynchronously, I start a new thread using Java executors so that lengthy calculations are done in a separate thread and response is sent right away.

Now does that ensure that I have freed the thread which had been processing my HttpServletRequest or is it still being used because a child thread is still running?

like image 749
hellojava Avatar asked Sep 17 '11 19:09

hellojava


People also ask

How many threads can servlet handle?

The performance is relative to the number of threads, not the number of instances of the servlet. For example, if there are 1000 requests, and the maximum threads that can be generated by servlet is 100, then there will be performance degradation.

What method of a servlet handles GET request?

An HTTP Servlet handles client requests through its service method.

Does each request in servlet runs in separate thread?

A: One thread per request.

What is proper way to handle servlets and thread safety?

To make a servlet or a block within a servlet thread-safe, do one of the following: Synchronize write access to all instance variables, as in public synchronized void method() (whole method) or synchronized(this) {...} (block only).


1 Answers

Per request means when an HTTP request is made, a thread is created or retrieved from a pool to serve it. One thread serves the whole request. Thread per connection would be the same thing except the thread is used for an entire connection, which could be multiple requests and could also have a lot of dead time in between requests. Servlet containers are thread per request. There may be some implementations that offer thread per connection, but I don't know, and it seems like it would be very wasteful.

Creating a thread inside another thread doesn't establish any special relationship, and the whole point of doing so in most cases is to let the one thread do more work or terminate while the other thread continues working. In your scenario, using a different thread to do work required by a request will, as you expect, allow the response to be sent immediately. The thread used to serve that request will also be immediately available for another request, regardless of how long your other thread takes to complete. This is pretty much the way of doing asynchronous work in a thread-per-request servlet container.

Caveat: If you're in a full Java EE container, threads may be managed for you in a way that makes it a bad idea to spawn your own. In that case, you're better off asking the container for a thread, but the general principles are the same.

like image 131
Ryan Stewart Avatar answered Oct 06 '22 12:10

Ryan Stewart