Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Context/Session Thread safety

 
Greg Lamonakis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Greg
 
ritesh hk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-Ritesh
 
James Christian
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic