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.
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.