Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple threads for each Client Request

 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

When client request comes a seperate thread is created by the Container along with a new request and response object. It is said that it is wrong to say that multiple instances are created for each request.

I am confused that if for each new client request a new request and response objects are created then are they referred to same instance which was created earlier? And does it happen for only single client?

Do let me know if there is a complete different picture.

Thanks in Advance!
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"new client request" means the request is new, not the client. Every time a request comes in, new request/response object is created and once the response (e.g. html or image etc) is sent to the client (e.g. browser) the request/response is destroyed.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that for each new request, a thread is created with new request and response objects and these separate thread operate on the same instance of the servlet.
They meant to say that there is only one instance of the servlet with multiple threads operating on them. That is why you should be careful in using the instance variables in the servlet class.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers.

@Thiyanesh - Would you please let me know what do you mean by "You should be careful using instance variables in servlet class".

Thanks again.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He means that you should not use member variables, because there's one servlet for multiple threads. ThreadA could update varA of servletA while ThreadB is reading the same variable of the same servlet, giving unexpected results.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in that case using Synchronization will help us. Will I be correct?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and No
Avoid the use of instance variables. There was something called "The Single Thread Model", but it's been deprecated. Synchronizing a block of code would still be acceptable though.
Other means could be to put values into the response, or the session, or the application context.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I have more questions...however I am still at chapter 3 in HSJS so I guess i would encounter such topics later on and get answers to my questions!

do let me know if this is not the case.

Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic