• 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

Thread safety and servlets

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always get confused about thread safety and servlets. I know multiple threads call doPost or doGet. here is an example:
public class MyServlet extends HttpServlet
{
privateHelper help;

public void init() throws ServletException
{
help = new Helper();
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException
{
help.helperMethod(req);
}
}
Helper is an instance variable of the servlet (which usually means not thread safe) but here the Helper class does not store state, the methods get the state needed passed in. Is this thread safe?
would it be better if i removed the class variable help and inside doPost i did
new Helper.helperMethod(req);
how about a static method instead:
Helper.helperMethod(req);
 
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
Does Helper have any instance variables that change between users? If Helper only uses local variables then you are safe. No reason to make a new one unless there is a problem with instance variables. Static would have the same effect as your single member - seems to me.
Bill
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway.
For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AHA! Yes, there IS a situation where multiple instances of the same servlet can exist, and the part of the spec that tells when and how is quoted here: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=007646
Which validates what I said above.
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim Holloway:
While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway.
For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.


Actually there are a lot of reasons to use instance methods in a stateless helper object. Foremost among these is that you can subclass your helper class and override the methods -- something you can't do with class methods.
Kyle
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Why does the servlet need to keep a reference to the helper class as an instance variable?
Why not just create a new local helper class in the method that it is needed?
If you are concerned that creating new helper objects when needed will have a performance hit then you could always have a pool of helper objects that would then be available to all servlets rather than just tying the object to a single servlet.
Thanks,
Steve
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think everyone here caught a part of it. I believe we have three different situations where a helper class may be used, and therefore several ways (patterns?) of implementing a solution. Anyone/everyone please correct me if I'm slightly misguided (or just flippin' wrong!).
Case 1
---------
Helper is doing processing related to all users of the system.
Example: Hit counter.
Solution: Use a Helper with a static member variable, and make the method used to change the variable synchronized in order to make it safe and produce correct results. An optional implementation is rather than making the Helper a member variable of the servlet, create it in the init() but place it in the ServletContext, and retrieve it in the doXXX processing. This would allow the Helper to be accessed by other servlets/JSPs as well.
Case 2
---------
Helper performs simple logic or utility processing on request input.
Example: Form Validation.
Solution: Instantiate a new Helper object in the doXXX method and use it as needed. Do not use static member variables in it.
Case 3
---------
Helper performs complex logic or utility processing on request input that involves creation of other objects to aid processing, or Helper needs to be part of a framework or extensible class hierarchy that supports/enforces certain functionality or the particular Helper do be used is picked dynamically based on request input or session/application state.
Example: Form validation with database update using a logging class and a database connection factory.
Solution: Write a Helper factory that creates and/or pools Helper objects. Additional objects that support the Helper (HelperHelpers?) may also need to be retrieved from a factory depending on requirements.
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic