• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question about thread safety and servlets

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic