This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make servlet Context attribute thread safe  RSS feed

 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Chrome Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently I was reading Head First JSP where it was explained that servlet Context attribute are not thread safe.I understood the concept.then it was also explained that how to make it thread safe.In which one solution given was to make service() method synchronized.but writer rejects this option saying that if we make service method synchronized it would stop other thread from same servlet accessing the context attributes,but it will not do anything to stop a thread from completly different servlet..Here friends its ok that it cccan not stop thread from other servlet but why it will not stop thread for same servlet.It mean that thread for same servlet are based on same object thats why they conflict each other.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2920
205
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

but why it will not stop thread for same servlet.It mean that thread for same servlet are based on same object thats why they conflict each other.

It will stop others to access the same servlet (and the servlet context attributes). Note that there will be one instance of a servlet.

Regards,
Frits
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!