• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question involving servlet output and synchronization

 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a bad idea to synchronize the output of a servlet, by creating a singleton class which handles the html?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Unnecessary synchronization will result in poor performance. Is this related to your other synchronization question?
 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes they are all related
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, how would one synchronize parts of a , servlet?
using the synchronized key word?
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use synchronized at all in servlets. Thread management is up to the container. You can get some way to the same behaviour that synchronized would give you by having your servlet implement the (now deprecated) interface javax.servlet.SingleThreadModel. I'd be curious to know why you need this functionality?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your servlet has an instance variable that must only be accessed by one request Thread at a time, of course you use synchronized code. When designing a servlet one normally strives to reduce or eliminate the necessity for synchronized access, but sometimes it is necessary.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic