File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Couple of question about servlets and thread sync 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 "Couple of question about servlets and thread sync" Watch "Couple of question about servlets and thread sync" New topic
Author

Couple of question about servlets and thread sync

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
1. When a servlet is loaded by the container, how many instance of servlets are created? As i understand only one instance is created or is it dependent on the servlet container's implementation ?
2. In page 195 of HFSJ book, it talks about synchronizing or not synchronizing of the service(), but the example given there is synchronizing the doXXX(). Did they mean to say that synchronizing the doXXX() as synchronizing the service() ?
3. Can someone give me a real life example where in there is a need for synchronizing the servletContext. It would be really helpful for me to digest that concept.

Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

1. When a servlet is loaded by the container, how many instance of servlets are created? As i understand only one instance is created or is it dependent on the servlet container's implementation ?

One instance of a servlet is created to serve the requests. Multiple instances will be created if your servlet implements SingleThreadModel (deprecated).
2. In page 195 of HFSJ book, it talks about synchronizing or not synchronizing of the service(), but the example given there is synchronizing the doXXX(). Did they mean to say that synchronizing the doXXX() as synchronizing the service() ?

Generally we are not supposed to override the service method. We override the doXXX methods. In that too generally only one of the doXXX methods service a request, so if a GET request is received, doGet method will handle that request, and doPost method will handle a POST request (you are free to call doPost from doGet and vice versa if you want, but I never do that). So if you synchronize the doXXX method(s), then that doXXX method will be able to handle only one request at a time. So if you synchronize the doGet method, then it will serve only one GET request at a time.
3. Can someone give me a real life example where in there is a need for synchronizing the servletContext. It would be really helpful for me to digest that concept.

Synchronizing on the ServletContext is seldom needed. If you are manipulating context scoped attributes and want to do it in a thread safe manner, you'll synchronize on ServletContext. For example if the number of hits on my application is stored in the ServletContext, then I'll do something like this to increase the hit counter


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Thanks for the reply.
My 2nd question was about the synchronizing the service method. Can you answer in terms of synchronizing ??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

sony agrwal wrote:My 2nd question was about the synchronizing the service method. Can you answer in terms of synchronizing ??

What type of answer are you expecting?? If you synchronize the service method (i.e. you override it which is generally not recommended), then the service method will handle only one request at a time. This is generally not recommended as this will slow down the response time of the servlet if it received multiple requests at the same time...
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
sony agrwal wrote:2. In page 195 of HFSJ book, it talks about synchronizing or not synchronizing of the service(), but the example given there is synchronizing the doXXX(). Did they mean to say that synchronizing the doXXX() as synchronizing the service() ?


I meant to ask "Does synchronizing service method means ..."


or
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

If you synchronize the service method, then your servlet will be able to serve only one request at a time. If you synchronize all the doXXX methods, then too your servlet will be able to serve only one request at a time...
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Okay... thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couple of question about servlets and thread sync
 
Similar Threads
multithreading in Servlets
Single Thread Model
Another Mock Exam Question
Servlet Lifecycle
Clarification on servlet based web service end point.