• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on thread safety and attributes

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following servlet code:



Which of the variables used in the above servlet reference objects that are thread safe? (Select two)
a req
b res
c session
d ctx
Answers: a and b

but i choose c,d.the reason why i choose c,d is -
"if we look at the 'session' and 'ctx' attributes from the perspective that they are local java objects ( for the time forgettting that they are HttpSession and ServletContext respectively ), then they seem to be thread safe."

but the author ( Hanumant Deshmukh page 441 - 442 ) says -

"Request and response objects are accessible only for the lifetime of a request, and so they are thread safe. Session and context objects can be accessed from multiple threads while processing multiple requests, which means they are not thread safe."

but he over looks that 'session' and 'ctx' are local and thus (seem to be) thread safe. !!

please help
thanks in advance !
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


huh !? who ever heard of access specifiers inside methods ?

I think those should be outside of the doGet() method. Also, even if they are private references (and you assign the session and context to them from doGet), you can get the session and context from another servlet in the same way. So that makes them not thread safe.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm....!!! john..!!! thanks a lot.
i never looked to the case that the local variables are declared private.
so even if the are declared out of doGet( )they become instance variables and thus arent thread safe !!

so the question is a bit wrong as the local variables are declared private
but neglecting that, the arguements to the method, i.e request and response are thread safe !
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic