• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

What does it mean - separate thread for each request?

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic