Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is servlet doGet and doPost method are threadsafe?

 
Kaleeswaran Karuppusamy
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is servlet doGet and doPost method are threadsafe or not?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are not thread safe. Either do not use any member variables in them (recommended) or use member variables (not recommended) and synchronize to give them thread safety
 
Kaleeswaran Karuppusamy
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know servlet not threadsafe i am asking doget and dopost method is thread safe or not
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are asking for the impossible.

One could easily write a servlet in which doGet and doPost are NOT threadsafe by simply having the methods modify instance variables so the answer is "it depends".

On the other hand, the local variables refering to the request and the response ARE isolated from all other Threads.

Bill
 
Prashant Hurria
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaleeswaran Karuppasamy wrote:i know servlet not threadsafe i am asking doget and dopost method is thread safe or not


Hun ....... What do you mean by thread-safety??? It is said that servlets are not thread safe that means if there are 10 Simultaneous requests for a particular servlet the container will simply invoke ten different threads who will invoke the service method(in turn do get and doPost) of the Servlet (Unless your servlet implements the Threadsafe interface).
Please be aware of the fact that Variables local to methods are always threadsafe that means each Thread gets its own copy of the Method-Local variables. Now the so called Threadsafety problem occurs when you are using a Member variable i.e. the variable you declare in the servlet class all methods and all Threads share these variables.Change done by one method or one thread will be seen to everyone. So if you are using this for inter-method or inter-thread communication its ok but if you accidental use it for
(Cliched Example -->) something like The Balance in a Bank Account where one thread is Depositing while the other is Withdrawing all hell break loose.

Hope that helps.
 
Kaleeswaran Karuppusamy
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this what i expected local variable are always thread safe...thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic