| Author |
multithreading in Servlets
|
Ram kovis
Ranch Hand
Joined: Jun 23, 2005
Posts: 130
|
|
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
Joined: Jan 10, 2002
Posts: 56185
|
|
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 ]
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Ram kovis
Ranch Hand
Joined: Jun 23, 2005
Posts: 130
|
|
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
Joined: Sep 11, 2005
Posts: 121
|
|
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.
|
keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
|
 |
Adrian Perry
Ranch Hand
Joined: Jul 21, 2005
Posts: 42
|
|
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.
|
 |
 |
|
|
subject: multithreading in Servlets
|
|
|