• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make servlet Context attribute thread safe

 
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
Pie
Posts: 2533
113
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic