Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about thread safety and servlets

 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a method thread safe if one writes a method within a servlet class and calls that method within a service method of that servlet class?
 
Basavaraju Banakar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No .. unless u handle explectly by synchronizing the access to the method...
 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was afraid that was the case. If a website has heavy volume, would you suggest openning those methods up and placing them directly into the service method, or would you suggest synchronizing them? Some perform (heavy and light)database transactions, while others just manipulate data from the user.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether your code is "inline" or in a method makes no difference to thread safety.
The answer depends on what your code is doing. Are you accessing data that is visible across multiple threads? Does your servlet have instance variables?
If your code only references thread-local storage, there are no problems.
[ February 19, 2004: Message edited by: Bear Bibeault ]
 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been reading on websites, that synchronization can increase performance when it is used with servlets. A lot of articles mention that synchronization used with a single instances of a class works a lot better than if each thread creates an instance of that class. This is suppose to reduce overhead, and garbage collection among the site. But I am still uncertian just because I have seen how crippling synchronization can be especial accross a network.
I read this article http://www.oop-reserch.com/tips.html and started to second guess my design.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether or not it will pay in performance to
1. create a single utility object and synchronize access to it
2. create a new utility object every time a request needs one
3. have a pool of utility objects
depends entirely on what the objects have to do - you just can't generalize and say one method is always best.
Object creation and GC is really quite fast in modern Java JVMs, especially if you do a little optimization of settings.
We see a pool of DB connection objects used frequently because creating a DB connection is expensive.
So my advice is - don't get all tangled up with trying to optimize when you don't even have any idea what is really consuming time in your particular application.
Bill
 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your absolutely right Will. It is all case dependent and at this given time I have no clue what resources are going to be consumed and which servlets will receive the most request. I am satified with that answer. Thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic