• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Couple of question about servlets and thread sync

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
My 2nd question was about the synchronizing the service method. Can you answer in terms of synchronizing ??
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... thanks
reply
    Bookmark Topic Watch Topic
  • New Topic