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. Advertising

  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. Advertising

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. David

    are static functions threadsafe?

    David, Jul 29, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    486
    David
    Jul 29, 2003
  2. =?Utf-8?B?QXJuZQ==?=

    Threadsafe VB

    =?Utf-8?B?QXJuZQ==?=, Apr 26, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,573
    Kevin Spencer
    Apr 27, 2005
  3. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  4. ppcguy
    Replies:
    1
    Views:
    606
    Andrea Desole
    Aug 8, 2005
  5. Replies:
    9
    Views:
    942
Loading...

Share This Page