• 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:

Multi-threading in servlets

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We know that when new client request comes for a servlet then container calls the service method from a new thread.

And service method always calls the get or post method.

So, my question is if we synchronize the get or post method and when client is working on that servlet then other client has to wait or not because of synchronization issues?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you synchronize service(), doGet() or doPost() methods, only one client request can be processed at a time.
So that's bad ...
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, my question is if we synchronize the get or post method and when client is working on that servlet then other client has to wait or not because of synchronization issues?



Absolutely, Synchronizing the entire service(), doGet() or doPost() method will kill concurrency. Doing so will cause extra delays and performance penalty. It's always advisable to use short synchronized blocks.

Regards,
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

synchronizing the service method kills the concurrency. So no other thread can run at the same time for the same servlet instance. However, I would like to add one note here :

If you are using STM for the synchronization, then, how your container vendor has implemetend the STM is important. Either a single servlet instance and make a queue for each request. Or multiple instance of the servlet and each handling the request seperately. Read Chap5 of HFSJ.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone using the Single threaded model should be smeared with bile fluid.

As for synchronization problems, synchronizing service() is pretty much the same as using STM. Synchronizing doPost() or doGet() is a little less worse, but worse never the less (assuming you sync doPost() but not doGet() or something like that).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic