• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronizing service method

 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I was reading book on SCWCD it says that if we make doGet or doPost method of the servlet synchronzed than server can serve only one request at a time(even if it has multiple instances). Can anybody explain this as service methid is instance method and on synchornizing it there will be instance lock and not a class lock. So other instance of the servlet should be allowed to invoke that method. Please explain.
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets only ever have one instance (unless of course they implement the SingleThreadModel) therefore if the service method is synchronized, new requests (i.e. new threads) will have to wait for the previous thread to finish.
HTH
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Faisal Khan:
Servlets only ever have one instance (unless of course they implement the SingleThreadModel)
HTH


Thanks Faisal
I think wrox Prfessional J2EE 1.3 Programming says that a servlet container may create more then one instance of a servelt(no restrictions of SingleThreadModel was mentioned). It also says that to optimize performance it is not uncommon for servlet container to create more than one instance. Also i dont think anywhere in Sun's specification they have said that a servlet container can create more than one instance only when the servelt is implementing SingleThreadModel and not otherwise. Correct me if i am wrong.

thanks
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
lets see if that helps.
1) Servlet container generally keeps a single instance of a servlet on memory, but its not a binding by specification.
For optimization purpose, it may create another instance, and do the processsing.
2) Implementing SingleThreadModel is one way to keep only a single worker thread executing a method on servlet instance, but if container decides to create another instance , then still many intances may work in tandom and may be called upon my worker threads (one at a time). So , single thread model doesnt mandate only a single instance.
SingleThreadModel doesnt mean single thread model of a servlet instance...its not a thread. It means single thread model of worker threads....if its singlethread model, a single worker thread will model on an instance.

3) On this I agree with Prakash. A synchronized method will ensure that a single thread works on the method. But if more instances are there, those many mehtods can be served at a time. Even I had this doubt in mind when I was studing, but while execution I didnt found any different situation,
please elaborate on this if possible.
When a method is synchronized, does it have only one existance(as static method) eventhough many instances are there? If yes then book is right, but if not then it can be simult. executed.

Thanks
amol
scjp
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the multi-threaded approach where a servlet not hosted in a distributed environment, the servlet container must use only one instance per servlet declaration. For each servlet request, the container spawns a separate thread which executes the servlet's service method.
If you synchronize the service method (or methods such as doGet or doPost which are dispatched to the service method of the HttpServlet abstract class), the servlet container must serialize requests through it. Therefore this approach is not recommended for performance reasons.
 
reply
    Bookmark Topic Watch Topic
  • New Topic