• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding ServletContext attributes?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn’t the Servlet spec developers just synchronize the get and set attribute methods in ServletContext to make the attributes thread safe ?
this way we need not synchronize on getServletContext in every servlet for e.g

Above question is from HFSJ page 194.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurpeet Singh,

Why didn't the Servlet spec developers just synchronize the get and set attribute methods in ServletContext to make the attributes thread safe ?
this way we need not synchronize on getServletContext in every servlet for e.g

I can think of two reasons:

1) performance penalty (it is up to developer if synchronizing is really needed )
2) If each individual method (getAttribute, setAttribute) is synchronized then you still might have a problem, because nothing prevents another thread from doing something in between those calls. (have a look at SCJP6 book Sierra & Bates chapter 9, page 714)

Regards,
Frits
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Fritz.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic