File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "SingleThreadModel " Watch "SingleThreadModel " New topic


Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
From Serv Spec.2.3

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

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
instance pool approach for what, when ?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
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.
Prasanna Wamanacharya
Ranch Hand

Joined: Apr 24, 2001
Posts: 143
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.
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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!.
nutan prakash

Joined: Oct 28, 2000
Posts: 16
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

P.Nutan Prakash<br />SCJP2
I agree. Here's the link:
subject: SingleThreadModel
jQuery in Action, 3rd edition