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?
Joined: Jul 18, 2002
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.
Joined: Feb 19, 2004
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.
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 ]
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.
Author and all-around good cowpoke
Joined: Mar 22, 2000
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
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.