Which of the following statements are true? Choose all apply.
1) Multiple instances created for a servlet implementing SingleThreadModel.
2) No more than one instance created for a servlet which is implementing STM.
3) Even static variable is Thread Safe for a servlet implementing STM.
4) If no threading model is implemented, the default a servlet is executed in multi-threaded model
What is the answer? I know, 4 is correct. What about 1 & 2? I thought 2 is correct, but their answer is 1 & 4. Is correct? Please Explain.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Instances of a servlet which implements SingleThreadModel will serve only one requeest at a time. So to allow multiple requests to be served by the servlet, the server creates multiple instances of a SingleThreadModel servlet. This is why option 1 is correct...
euch I think this conclusion is wrong.
the way STM is implemented depends on your container
It may create multiple instances of the servlet or it may queue the
requests to pass them one at the time to 1 servlet it depends...
I'm just pretty sure that only answer 4 is correct and your exam is wrong.
I shall quote Head First Servlets and Jsp page 201 if I may...
But how does the web container guarantee a
servlet gets only one request at a time?
The web container vendor has a choice. The container
can maintain a single servlet, but queue every request and
process one request completely before allowing the next
request to proceed. Or the container can create a pool of servlet instances and process each request concurrently,
one per servlet instance.
STM interface ensures that servlet handles only one request at a time. You are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
So, the answer is only 4. And the mock exam is wrong in answer.
subject: STM and instances of servlets - Whizlabs Question