• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets and Threads

 
vinoth khanna
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to make my servlet methods get and post with threading.
What if I use only synchronized on my get method.




or must I achieve threading in other ways or how(want sample code e.g.. Must I overide servlet API's for Weblogic 7.0.)

Actually..I am reading a file from servlet ,if multiple access comes to the servlet simultaneously, it gives Exceptions.How can I stop that.

And a servlet accessed from one machine simultaneously (above 3) gives problem,but different nodes(above 6 users) doesn't. Why is that so and wat is behind that..

How to bring performance in my servlet case.

Suggest whether threading will work out fro my case.

Thanx All,
Vin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets have to be made thread safe. The servlet container runs two or three or a thousand user requests through the same servlet at the same time, so you have to be careful. The simplest rule is to not use any member variables. The arguments passed to a method and the local variables defined within a method are all safe enough. If you're running into collisions between users, I'd bet you are using member variables or class variables (static) or some shared resource outside the servlet, like a file. Is that the case?
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not synchronize on any of the service methods!!! This is very similar to implementing the SingleThreadModel, which has been depreciated because it is spectacularly un-thread safe!

Instead, create a synchronized block using whatever object you are manipulating as the lock. I will try and post some example code when I get a couple of spare minutes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!