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 !
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ] Performance is a compulsion, not a option, if my existence is to be justified.
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.
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 !