• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multithreading in Servlets

 
Ram kovis
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, where should I ask this question


Do not synchronize doGet, doPost
The below article says

The servlet specification strongly recommends that doGet, doPost, and all other service methods should not be declared as synchronized.
Declaring such methods as synchronized will often decrease performance significantly. It is not necessary to declare these methods as synchronized at all, since this is taken care of by the servlet container itself.


My question is, do you guys really write the code keeping in mind of this multi threading?..
As the above article says, I really never bothered to use synchronize etc.. just write doget or doPOst().. thats it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. All servlets, or code called from servlets, needs to be written with thread safety in mind.

The use of synchronization to achieve this is rarely necessary, and never at the gross level of the doGet/doPost methods.
[ September 13, 2005: Message edited by: Bear Bibeault ]
 
Ram kovis
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

The use of synchronization to achieve this is rarely necessary, and never at the gross level of the doGet/doPost methods.

[ September 13, 2005: Message edited by: Bear Bibeault ]


That is what , I wanted to knnow, whether anybody use "sunchronize" explicity..
thanks for your reply Bear!!
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not synchronize doGet() / doPost() methods. It is a bad practice. It has more harmful effects compared to the uses and is discouraged. Even the SingleThreadModel interface has been deprecated from the API.

In most practical situations, you do not have to worry about multi-threading effects, though in certain situations you have to keep in mind that servlets are multi-threaded applications. For example, you should know that servlet instance variables are shared by multiple threads.

MORAL: Build in thread-safety in your code by synchronizing on context or session and do not synchronize the doXXX methods.
 
Adrian Perry
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

To answer ur question....

Consider a ServerContext object, which u have used to set an attribute. It is a known fact that this ServerContext object works at application level and so many servlets can hit this ServerContext object. So if we set the attribute to a particular value, it is possible that it may undergo a change via a different servlet, which is undesirable.

So we should sychronize this ServerContext object.
As a thumb rule, We do not require to synchronize request object but we should take care of ServerContext and session object.
But it should be noted that synchronizing a method allows only one thread to access the given method, thus sacrifysing concurrency. So we should be very careful while using 'synchronizing' keyword and should only use it where it is utmost needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic