Forums Register Login

Context Attributes are not thread safe??

+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Gowher and Satau,

Thanks for that..I understood now...
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1064 times.
Similar Threads
Thread safe
locking and others
thread safe
Session
Thread Safety in Servlets
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:22:36.