Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi-threading in servlets

 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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?
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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 ...
 
Kunal Jag
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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,
 
Amit Prakash Singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic