Two Laptop Bag
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Context/Session Thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Context/Session Thread safety" Watch "Context/Session Thread safety" New topic

Context/Session Thread safety

Greg Lamonakis

Joined: Mar 18, 2005
Posts: 4
Can anyone tell me if these two code snippets do the same thing and if not what the specific difference is:

synchronize(getServletContext()) {

compared to

ServletContext mySC = getServletContext();

synchronize(mySC) {

And is the answer the same for synchronizing on sessions (I would assume so but want to be sure) as listed below:

synchronize(getSession()) {

HttpSession mySession = getSession();

synchronize (mySession) {

Thanks for your time,
ritesh hk

Joined: Mar 15, 2005
Posts: 6
I dont see any difference there. You could test it out buddy. It should be fine I guess. In both cases the same object is beign referenced.


-------------<br />SCJP 1.4 <br />SCWCD (just started)
James Christian
Ranch Hand

Joined: Apr 04, 2005
Posts: 63
The only difference is that in the first your reference to the context expires at the end of your synchronised code block. But I don't see how knowing this or not knowing it would help or deter you in the exam.
I agree. Here's the link:
subject: Context/Session Thread safety
It's not a secret anymore!