Hi I ran into this question in J2EECertificate: Given that a servlet implements the SingleThreadModel interface. Which of the following statements are true? [Select all correct answers] a) The servlet is thread-safe. b) The servlet is distributable across multiple JVMs. c) The servlet container guarantees that no two threads will execute concurrently in the servlet's service method. d) The servlet container may create multiple instances of the servlet and dispatch each servlet request to a different servlet instance. e) The servlet must implement the methods defined in the SingleThreadModel interface. The answer given is c and d. I think that the c option is if not incorrect then at least ambiguous. If the container creates two instances of the same servlet then as I understand it does not guarantee that only a single thread may execute the servlet's service() method. It does guarantee that two threads will no execute the servlet instance's service() method.
IMO, I think c is correct as it is. If you don't implement the SingleThreadModel interface, you might have n threads running the same service() on the same servlet instance. But, if you implement this interface, you may have different instances, each one executing its own service() method. It still not guarantee that is thread safe, since you can have static variables, for instance.
The servlet container guarantees that no two threads will execute concurrently in the servlet's service method.
"no two threads". An instance will be use to execute a single thread. not many threads at the same time. The container creates many instances to give the impression of concurrent execution. hope it helps
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
My problem with that option is that it does not say anything about instances. There is a possibility that two threads request the same servlet, so the container uses different instances for these two threads. They are not executing the same instance's service() method, but I think they are executing the same servlet's service() method. If the wording would be something like this I would have no problem with that option: c) The servlet container guarantees that no two threads will execute concurrently in the servlet instance's service method.
The SingleThreadModel interface is basically an agreement between the container and the developer to make sure that for a servlet that implements the interface, "no two threads will execute concurrently in the servlet's service method". However, it is up to individual vendors how they wish to implement this behaviour. If a vendor only provides a single instance of a servlet running in the container, synchronizing access to the single instance of the servlet is one way to satisfy this agreement. An alternative approach that a vendor could take is for the container to maintain a pool of servlet instances. Instead of synchronizing access to each servlet, the container would dispatch each new request to a free servlet. The behaviour of the servlet from the developer's point of view is the same regardless of the implementation.
My problem with that option is that it does not say anything about instances.