• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

doubt regarding ServletContext attributes?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • 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.
 
Creator of Enthuware JWS+ V6
Posts: 3346
303
Android Eclipse IDE Chrome
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Fritz.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic