• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multithreading in Servlets

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic