Member variables within servlet application - are they threadsafe?

Discussion in 'Java' started by Simon Eichenauer, Jun 25, 2007.

  1. Hi,

    I´ve got a question about thread safety within servlet applications.
    I know that, for example within a servlet or a struts action, you
    shouldn´t use member variables because within the virtual machine
    only one instance of each servlet / action exists (or at least the member
    variables exist only once and aren´t put on the thread stack). But what
    about member variables declared within
    classes that are used within servlet or action methods? Are they threadsafe?
    For example:

    Action
    execute() {

    Test test = new Test();
    test.setMemberVariable("test");
    }

    Class Test {
    private String member; // Threadsafe ???

    public void setMemberVariable(String test) {
    member = test;
    }
    }

    Maybe it´s really stupid but I just couldn´t find any hint.

    Regards,
    Simon
     
    Simon Eichenauer, Jun 25, 2007
    #1
    1. Advertisements

  2. "Simon Eichenauer" <> wrote in message
    news:f5pdb7$7nc$-hagen.de...
    | Hi,
    |
    | I´ve got a question about thread safety within servlet applications.
    | I know that, for example within a servlet or a struts action, you
    | shouldn´t use member variables because within the virtual machine
    | only one instance of each servlet / action exists (or at least the member
    | variables exist only once and aren´t put on the thread stack). But what
    | about member variables declared within
    | classes that are used within servlet or action methods? Are they
    threadsafe?
    | For example:
    |
    | Action
    | execute() {
    |
    | Test test = new Test();
    | test.setMemberVariable("test");
    | }
    |
    | Class Test {
    | private String member; // Threadsafe ???
    |
    | public void setMemberVariable(String test) {
    | member = test;
    | }
    | }
    |
    | Maybe it´s really stupid but I just couldn´t find any hint.

    Your new test object's reference is held locally to the thread and if you do
    not allow the reference to be stored where it could be accessed by another
    thread, the object will be threadsafe because other threads cannot access
    it. Its methods will only be activated from the original thread. If you
    store the reference in some structure that is accessible to other threads
    (static variables, servlet instance variables, any data structure referenced
    by a servlet instance variable, etc,) you expose it to being used by two
    threads at the same time. In short, you can't determine threadsafety by
    looking at the class--you have to look at how its used.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Jun 26, 2007
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,810
    Gandalf
    Feb 8, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    2,418
    Jack Jia
    Apr 4, 2004
  3. ppcguy
    Replies:
    1
    Views:
    762
    Andrea Desole
    Aug 8, 2005
  4. Asfand Yar Qazi
    Replies:
    4
    Views:
    691
    Asfand Yar Qazi
    Nov 12, 2004
  5. Martin M.
    Replies:
    4
    Views:
    525
    Simon Brunning
    Dec 15, 2005
  6. Dave Rudolf
    Replies:
    1
    Views:
    489
    Kai-Uwe Bux
    May 17, 2006
  7. Replies:
    9
    Views:
    1,326
  8. David A. Black
    Replies:
    2
    Views:
    354
    Tim Hunter
    Aug 19, 2004
Loading...