• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on thread safety and attributes

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic