This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

What does it mean - separate thread for each request?  RSS feed

 
Ranch Hand
Posts: 514
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!
 
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.

 
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.
 
Bin Smith
Ranch Hand
Posts: 514
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.
 
Author and ninkuma
Marshal
Posts: 66783
168
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!