wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Context Attributes are not thread safe?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Context Attributes are not thread safe??" Watch "Context Attributes are not thread safe??" New topic
Author

Context Attributes are not thread safe??

Vinay Kumar chowdary
Greenhorn

Joined: Nov 24, 2006
Posts: 20
Hi,

I came across these while reading HFSJ:

The best way is to synchronize on the ServletContext object.
synchronized (getServletContext())
This make the context thread-safe ONLY if ALL the servelts synchronize on the context.


However, I am bit confused abt the last line "This make the context thread-safe ONLY if ALL the servelts synchronize on the context"

can anyone explain this?

Thanks,
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Context Attributes are not thread safe.
You can make Context Attributes thread safe if you synchronize on ServletContext object.
Context Attributes will be thread safe if all servlets accessing context attributes are synchronized.
Check yourself by synchronize context object in one servlet and in another servlet dont synchronize context object and see result.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It means that if you forget to do this in at least ONE of the servlets, then it's not thread safe anymore. It has to be done in ALL servlets in the application.


[My Blog]
All roads lead to JavaRanch
Vinay Kumar chowdary
Greenhorn

Joined: Nov 24, 2006
Posts: 20
Hi Gowher and Satau,

Thanks for that..I understood now...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Context Attributes are not thread safe??
 
Similar Threads
Thread Safety in Servlets
locking and others
Thread safe
thread safe
Session