jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt regarding ServletContext attributes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt regarding ServletContext attributes?" Watch "doubt regarding ServletContext attributes?" New topic
Author

doubt regarding ServletContext attributes?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

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

Joined: Apr 04, 2012
Posts: 924
    
    1

Thanks for the reply Fritz.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt regarding ServletContext attributes?
 
Similar Threads
Confusion in code
Thread safe
Context Attributes are not thread safe??
Question on attributes
someone pls explain me this statement...