If a servlet implements Single thread model interference ,How servlet container ensure that servlets handle only one request at a time ? Even single thread model interface has no methods too.Thanks.
+1
A:
The servlet container creates a pool of servlet instances, and keeps track of which instances are currently "in use". If all of the instances are "in use" when a new request comes in, the container could either wait for an existing one to become free, or create a new instance to handle the request.
The single thread model isn't widely used though - it's better to make the servlet itself stateless, and allow several requests to be handled simultaneously.
Jon Skeet
2009-03-15 08:59:04
Please explain why STM is not widely used?
Warrior
2009-03-15 09:06:50
You need to work out any threading issues elsewhere anyway - you can't store any sensible mutable state in the servlet instance, as there will be multiple instances - so you might as well use the simpler threading model to start with. At least, that's my take on it.
Jon Skeet
2009-03-15 09:11:36
So we can have a local variable within the service method rather than instance variable
Warrior
2009-03-15 09:25:03
If you use an instance variable, you're pretending your object has state relevant to the object rather than just the request. It would be better to encapsulate that state in its own type, IMO.
Jon Skeet
2009-03-15 09:32:55
A:
It's implementation-dependent. It can create the pool of servlet instances or else it can just have a single servlet instance and synchronize access to it. The latter leads to more contention.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/SingleThreadModel.html
Willie Wheeler
2009-03-15 09:03:37