• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does it mean - separate thread for each request?

 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Can some one explain if there is a difference in:
1. Separate instance per each request.
2. Separate thread with the same instance per each request.

I am about http request and servlet.

Thank you!
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Can some one explain if there is a difference in:
1. Separate instance per each request.
2. Separate thread with the same instance per each request.

I am about http request and servlet.

Thank you!


Separate object per request means a separate object of something is created per request. So each request has its own object, and hence its own copy of member variables. Like for instance in struts, when we code action classes, each request gets its own object of the action class.

Separate threads that share an object means separate threads have the same object reference and hence they share the object's data. Like for instance a servlet is instantiated only once. When a new request comes in, another thread is created to cater to it.

All these different threads ( in the case of servlets ) share the same object and its data. This is one reason why we don't create member variables in servlets ( unless we don't care about accessing them in a thread unsafe way). What we instead do is create request and session attributes which are unique per request and session respectively. This is different from the action classes of struts where each request gets its own action class object.

Disclaimer - I'm fairly new to these topics. Have recently started learning them ( about two days back or so ). But this is what I've found out so far.

 
Surendra Kumar
Ranch Hand
Posts: 236
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Separate instance means creating a new servlet object every time
2. Separate thread for each request means only creating a new thread and call doGet() or doPost() methods. In this case the servlet class is already loaded by the container.
The container manages the life cycle of the threads and servlet.
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is new thread created per each request to call doGet or doPost when servlet instance is the same?
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Why is new thread created per each request to call doGet or doPost when servlet instance is the same?


You need an object of a class to invoke it's method. Isn't it. So the container creates one object and gives it to all the requests.

So different objects ( different request objects ) require the same servlet object. How do you do that? Create one thread per request.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Why is new thread created per each request to call doGet or doPost when servlet instance is the same?

If multiple threads were not created, each request would have to wait in line to execute while any previous requests executed. By using multi-threading, requests can execute independently.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the right reason, Bear.

I know Volodymyr has his share of thanks to say; I have mine too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!