For servlets not implementing the SingleThreadModel interface, if the service method (or methods such as doGet or doPost which are dispatched to the service method of the HttpServlet abstract class) has been defined with the synchronized keyword, the servlet container cannot use the instance pool approach, but must serialize requests through it.
. Does it mean that it will be error if one try to use instance pool approach?. Bye. Viki.
hi, what i understand is the instance pooling approach is not in our hand to manage. its managed by the servlet engine. so if we have declared service() method with synchronized keyword then it has to serialize all the requests as otherwise it won't be a thread-safe approach. this is because service() method is an entry point to the servlet for any request. but as recommended we should not do something like this instead make the servlet implementing SingleThreadModel. i dont understand the reason for that though regards maulin.
First point is in the quoted statement, the author is talking about only those servlets that donot implement SingleThreadModel interface. The inference from this point is that the requests for this servlet are going to handled by single servlet. If the servlet's service method is not synchronised then multiple requests could be handled by the same instance. But if the service method is synchronised the instance would be able to handle only one request at a single instance. To enable the multiple request handling it must serialise or store the request in memory to handle other requests. I couldnot think of any other thing........... just a guess Kindly correct me, if I am wrong.
Hi, After going through the specs, I think the foll. two statements might help in understanding the SingleThreadModel- Servlet NOT implementing SingleThreadModel : Multiple threads, Single instance Servlet implementing SingleThreadModel : Single thread, Multiple instances
Please correct me if I am wrong. Prasanna. SCJP2
Joined: Sep 02, 2001
Well, i concluded that: Threads are one of the major task to handle in all times.I think there is NO any exact algorithm to handle threads in Servlets.However it is totally based on arhitecture design.And architecturing any thing 90% depends on past experience.Servlet specification therefore just suggest about thread how to hande NOT recommend!. Bye. Viki.
Hi, Hope the following explanation could resolve most of the doubts. Let us look at the ONLY possible 4 scenarios... 1. Servlet does NOT implement SingleThreadModel and the service method is NOT synchronized In this case The servlet engine will create ONLY ONE instance of the servlet. And all the requests are executed in multiple threads. Suppose the first request executes 20% of the service method, then the second request executes 30% of the service method... happens this way. So, here the servlet method is not thread safe. Since the service method is executed synchronously for all the requests. 2. Servlet does not implement SingleThreadModel and the service method is synchronized The case here is same as above. Except that here the service method is sync. therefore all the requests will be lined up and the service method for each will be executed in the order they were recieved. The servlet spec. suggests that the service method should NOT be sync. since this will lead to low performance. (evident) 3. Servlet implements SingleThreadedModel and the service method is NOT synchronized. Here the servlet engine will create multiple instances of the servlet (pool) and execute the requests. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet’s service method. 4. Servlet implements SingleThreadedModel and the service method is synchronized. This scenario is not discussed anywhere in the Servlet Spec. But if this is done, the result would be the same as if the SingleThreadedModel interface is implemented. Since, by definition if a servlet implements the SingleThreadedModel it ENSURES that the SAME service method is NOT executed concurrently by two different requests. - Nutan