• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting a new thread for every request.

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2437
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2437
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic