wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Starting a new thread for every request. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Starting a new thread for every request." Watch "Starting a new thread for every request." New topic
Author

Starting a new thread for every request.

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Servlet is instantiated only once, then for every subsequest request a new thread is created to serve it. My questions are:

1. How is it possible to start multiple threads on a single servlet instance.
I mean: servletInstance.start()- Why it doesn't give IllegalThreadStateExcption?

2. Which class or interface in the hierarchy of javax.servlet.http.HttpServlet extends or implents Runnable interface. Where is run() method defined, who calls it and when?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1686
    
  25

Hi Swaraj Gupta,

1. How is it possible to start multiple threads on a single servlet instance.
I mean: servletInstance.start()- Why it doesn't give IllegalThreadStateExcption?

The threads are not created inside the Servlet. For every request a seperate thread is created by the container. The request-threads will all access the single Servlet's instance service() method

This is what the Servlet 2.4 specs says:
SRV.2.1 Request Handling Methods
The handling of concurrent requests to a Web application generally requires
that the Web Developer design servlets that can deal with multiple threads executing
within the service method at a particular time.
Generally the Web container handles concurrent requests to the same servlet
by concurrent execution of the service method on different threads.


Which class or interface in the hierarchy of javax.servlet.http.HttpServlet extends or implents Runnable interface. Where is run() method defined, who calls it and when?

There is no such class because of the previous explanation (and you can verify this from the API)

Have a look at Servlet 3.0 specs as well.

Regards,
Frits
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Hi Frit,
thanks for the reply.

The threads are not created inside the Servlet. For every request a seperate thread is created by the container. The request-threads will all access the single Servlet's instance service() method


If am not wrong, you mean to say container keeps the reference to the servlets object with it. And it uses the same object reference to call the service method for each request from client.

Is it so?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1686
    
  25

Yes!
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Thanks Frits !
 
GeeCON Prague 2014
 
subject: Starting a new thread for every request.