File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes A question about thread safety and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A question about thread safety and servlets" Watch "A question about thread safety and servlets" New topic
Author

A question about thread safety and servlets

Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
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

Joined: Jul 18, 2002
Posts: 25
No .. unless u handle explectly by synchronizing the access to the method...


It is impossible to make anything foolproof because fools are so ingenious..Murphy<br />Basu.
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
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

Joined: Jan 10, 2002
Posts: 61761
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Feb 19, 2004
Posts: 27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about thread safety and servlets