Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in ThreadSafety

 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

There was a question in whizlabs asked against threadsafety

if a servlet implements SingleThreadModel is it Thread Safe??

or only Instance Varaibles are threadsafe??
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare a servlet as being single threaded, then the instance fields of the servlet are thread safe. I am not sure but I think the servlet container can create multiple instances of the servlet to respond to the requests. So the static fields will not be thread safe any ways. I don't know what the servlet spec says about this...
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely, the container can create multiple instances, each servicing only one request. This makes the instance variables thread safe.
But the static variables are still thread unsafe.

Also, note that this multiple instance creation is not guaranteed by the spec. It is vendor dependent.
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so
i would say even servlet Implements SingleThreadModel only instance varables are thread safe not the servlet is it??
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways to introduce thread-unsafety into a web app besides servlet instance methods, e.g. sessions and context attributes. That's why that interface is deprecated - it doesn't guarantee thread-safety.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic