servlet reference and thread safe

Discussion in 'Java' started by gk, Nov 10, 2006.

  1. gk

    gk Guest

    "...the instance , static variables used in a servlet reference
    object are not thread safe..."



    why instance variables are not thread safe ?

    say i have

    class MyServlet extends HttpServlet
    {
    Sting x;
    String y;

    //doGet()

    //doPost()

    }


    How , this x,y cant be thraed safe ?

    seperate instance will have there own copy and hence they are not
    going to be mixed up in multithreading enviornment.
    so, these variables are thread safe .

    could you please explain ...why that statement is correct ?
     
    gk, Nov 10, 2006
    #1
    1. Advertising

  2. gk

    hiwa Guest

    gk wrote:
    > "...the instance , static variables used in a servlet reference
    > object are not thread safe..."
    >
    >
    >
    > why instance variables are not thread safe ?
    >
    > say i have
    >
    > class MyServlet extends HttpServlet
    > {
    > Sting x;
    > String y;
    >
    > //doGet()
    >
    > //doPost()
    >
    > }
    >
    >
    > How , this x,y cant be thraed safe ?
    >
    > seperate instance will have there own copy and hence they are not
    > going to be mixed up in multithreading enviornment.
    > so, these variables are thread safe .
    >
    > could you please explain ...why that statement is correct ?


    > this x,y cant be thraed safe ?

    No. They can't be, unless their accesses are synchronized.
    Only local variables in methods are thread safe because local variables
    are allocated on the stack per each call.
     
    hiwa, Nov 10, 2006
    #2
    1. Advertising

  3. gk

    gk Guest

    hiwa wrote:
    > gk wrote:
    > > "...the instance , static variables used in a servlet reference
    > > object are not thread safe..."
    > >
    > >
    > >
    > > why instance variables are not thread safe ?
    > >
    > > say i have
    > >
    > > class MyServlet extends HttpServlet
    > > {
    > > Sting x;
    > > String y;
    > >
    > > //doGet()
    > >
    > > //doPost()
    > >
    > > }
    > >
    > >
    > > How , this x,y cant be thraed safe ?
    > >
    > > seperate instance will have there own copy and hence they are not
    > > going to be mixed up in multithreading enviornment.
    > > so, these variables are thread safe .
    > >
    > > could you please explain ...why that statement is correct ?

    >
    > > this x,y cant be thraed safe ?

    > No. They can't be, unless their accesses are synchronized.
    > Only local variables in methods are thread safe because local variables
    > are allocated on the stack per each call.



    if i make synchronized setter/getter methods to access those instance
    variables , will that be thread safe now ?
     
    gk, Nov 10, 2006
    #3
  4. gk

    hiwa Guest

    gk wrote:
    > hiwa wrote:
    > > gk wrote:
    > > > "...the instance , static variables used in a servlet reference
    > > > object are not thread safe..."
    > > >
    > > >
    > > >
    > > > why instance variables are not thread safe ?
    > > >
    > > > say i have
    > > >
    > > > class MyServlet extends HttpServlet
    > > > {
    > > > Sting x;
    > > > String y;
    > > >
    > > > //doGet()
    > > >
    > > > //doPost()
    > > >
    > > > }
    > > >
    > > >
    > > > How , this x,y cant be thraed safe ?
    > > >
    > > > seperate instance will have there own copy and hence they are not
    > > > going to be mixed up in multithreading enviornment.
    > > > so, these variables are thread safe .
    > > >
    > > > could you please explain ...why that statement is correct ?

    > >
    > > > this x,y cant be thraed safe ?

    > > No. They can't be, unless their accesses are synchronized.
    > > Only local variables in methods are thread safe because local variables
    > > are allocated on the stack per each call.

    >
    >
    > if i make synchronized setter/getter methods to access those instance
    > variables , will that be thread safe now ?

    Yes.
     
    hiwa, Nov 11, 2006
    #4
  5. gk

    Daniel Pitts Guest

    gk wrote:
    > hiwa wrote:
    > > gk wrote:
    > > > "...the instance , static variables used in a servlet reference
    > > > object are not thread safe..."
    > > >
    > > >
    > > >
    > > > why instance variables are not thread safe ?
    > > >
    > > > say i have
    > > >
    > > > class MyServlet extends HttpServlet
    > > > {
    > > > Sting x;
    > > > String y;
    > > >
    > > > //doGet()
    > > >
    > > > //doPost()
    > > >
    > > > }
    > > >
    > > >
    > > > How , this x,y cant be thraed safe ?
    > > >
    > > > seperate instance will have there own copy and hence they are not
    > > > going to be mixed up in multithreading enviornment.
    > > > so, these variables are thread safe .
    > > >
    > > > could you please explain ...why that statement is correct ?

    > >
    > > > this x,y cant be thraed safe ?

    > > No. They can't be, unless their accesses are synchronized.
    > > Only local variables in methods are thread safe because local variables
    > > are allocated on the stack per each call.

    >
    >
    > if i make synchronized setter/getter methods to access those instance
    > variables , will that be thread safe now ?


    Or, if the referenced objects are threadsafe (String objects are), then
    you can use AtomicReference.
     
    Daniel Pitts, Nov 11, 2006
    #5
  6. "gk" <> writes:

    >
    > if i make synchronized setter/getter methods to access those instance
    > variables , will that be thread safe now ?
    >


    No. Though I understand how 'yes' is tempting. Because
    it would surely be easier that way.




    class Subtractor {

    private int subtrahend;
    private int minuend;

    public synchronized int getSubtrahend() {
    return subtrahend;
    }

    public synchronized void setSubtrahend(int s) {
    this.subtrahend = s;
    }

    public synchronized int getMinuend() {
    return this.minuend;
    }

    public synchronized void setMinuend(int m) {
    this.minuend = m;
    }

    public synchronized int difference() {
    return minuend - subtrahend;
    }
    }


    Let's say you have multiple threads
    accessing the same Subtractor object.
    Let's call them Thread 1 and Thread 2, because
    I'm writing this at 3:30 AM.

    This is a possible flow:

    Thread 1: setMinuend(5)
    Thread 1: setSubtrahend(2)
    Thread 1: difference() == 3
    Thread 2: setMinuend(10)
    Thread 2: setSubtrahend(8)
    Thread 2: difference() == 2

    Hooray!

    Unfortunately, this is also a possible
    outcome:

    Thread 1: setMinuend(5)
    Thread 1: setSubtrahend(2)
    Thread 2: setMinuend(10)
    Thread 2: setSubtrahend(8)
    Thread 1: difference() == 2
    Thread 2: difference() == 2

    Ooops.


    Why didn't the synchronized voodoo help? It did it's job--
    no two threads were allowed to access a getter or setter
    at the same time. Unfortunately, that's completely pointless.
    We only get the right answer if setMinuend(), setSubtrahend,
    and difference() aren't all called by the same thread without
    any interruption.


    Go forth and sin no more.

    --
    Mark Jeffcoat
    Austin, TX
     
    Mark Jeffcoat, Nov 11, 2006
    #6
  7. gk

    Simon Brooke Guest

    in message <>, gk
    ('') wrote:

    > "...the instance , static variables used in a servlet reference
    > object are not thread safe..."
    >
    >
    >
    > why instance variables are not thread safe ?


    Because the same instance may be doing different things with different data
    in different threads.

    Suppose you have a servlet (as, indeed, I do) which serves content from a
    database. It may be called thus:

    http://www.jasmine.org.uk/dogfood/story/article_31.html

    or thus:

    http://www.jasmine.org.uk/dogfood/story/article_42.html

    The two calls must deliver different content. Suppose, in order to serve an
    article, I stored the article index on an instance variable. Suppose the
    request for article 42 comes in from Fred a few thousandths of a second
    before the request for article 31 comes in from Joe.

    So the servlet sets the instance variable to 42 in the thread that's
    servicing Fred's request, and then goes to look in the database. But
    before it can construct the query the thread that's servicing Joe's
    request overwrites the instance variable with 31. So Fred gets sent
    article 31, not article 42 as he requested.

    > How , this x,y cant be thraed safe ?


    That's right.

    > seperate instance will have there own copy and hence they are not
    > going to be mixed up in multithreading enviornment.
    > so, these variables are thread safe .


    Yes, but you don't have separate instances (normally). You have one
    instance and separate threads. You can have separate instances but it's
    very extravagant in terms of machine resources.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/
    Ring of great evil
    Small one casts it into flame
    Bringing rise of Men ;; gonzoron
     
    Simon Brooke, Nov 11, 2006
    #7
  8. gk

    Simon Brooke Guest

    in message <>, gk
    ('') wrote:

    >
    > hiwa wrote:
    >> No. They can't be, unless their accesses are synchronized.
    >> Only local variables in methods are thread safe because local variables
    >> are allocated on the stack per each call.

    >
    >
    > if i make synchronized setter/getter methods to access those instance
    > variables , will that be thread safe now ?


    No. You /cannot/ use instance variables of the Servlet dynamically in
    serving requests. At all. Ever. You can use them to hold configuration
    data. You can also create separate per-service objects in which to pass
    around the context of the request (after all, this is essentially what
    HttpServletRequest and HttpServletResponse are, and there's nothing to
    prevent you creating a class of your own); and you can use the instance
    variables of these per-service objects if you want to.

    But you cannot use instance variables of the Servlet, because you don't
    know how different threads will interleave their actions.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    ;; Life would be much easier if I had the source code.
     
    Simon Brooke, Nov 11, 2006
    #8
  9. gk

    Simon Brooke Guest

    in message <>, hiwa
    ('') wrote:

    >
    > gk wrote:
    >> hiwa wrote:
    >> > gk wrote:
    >> > > "...the instance , static variables used in a servlet reference
    >> > > object are not thread safe..."
    >> > >
    >> > >
    >> > >
    >> > > why instance variables are not thread safe ?
    >> > >
    >> > > say i have
    >> > >
    >> > > class MyServlet extends HttpServlet
    >> > > {
    >> > > Sting x;
    >> > > String y;
    >> > >
    >> > > //doGet()
    >> > >
    >> > > //doPost()
    >> > >
    >> > > }
    >> > >
    >> > >
    >> > > How , this x,y cant be thraed safe ?
    >> > >
    >> > > seperate instance will have there own copy and hence they are not
    >> > > going to be mixed up in multithreading enviornment.
    >> > > so, these variables are thread safe .
    >> > >
    >> > > could you please explain ...why that statement is correct ?
    >> >
    >> > > this x,y cant be thraed safe ?
    >> > No. They can't be, unless their accesses are synchronized.
    >> > Only local variables in methods are thread safe because local
    >> > variables are allocated on the stack per each call.

    >>
    >>
    >> if i make synchronized setter/getter methods to access those instance
    >> variables , will that be thread safe now ?

    > Yes.


    Errr... no. And if you think it is, you /really/ don't understand how a
    Servlet engine works.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    ;; Quidquid latine dictum sit, altum sonatur.
     
    Simon Brooke, Nov 11, 2006
    #9
  10. gk

    Lew Guest

    >> gk wrote:
    >>>> gk wrote:
    >>>>> "...the instance , static variables used in a servlet reference
    >>>>> object are not thread safe..."
    >>>>>
    >>>>>
    >>>>>
    >>>>> why instance variables are not thread safe ?
    >>>>> seperate instance will have there own copy and hence they are not
    >>>>> going to be mixed up in multithreading enviornment.
    >>>>> so, these variables are thread safe .
    >>>>>
    >>>>> could you please explain ...why that statement is correct ?
    >>>>> this x,y cant be thraed safe ?

    ....

    >>> if i make synchronized setter/getter methods to access those instance
    >>> variables , will that be thread safe now ?


    >> hiwa wrote:
    >> Yes.


    Simon Brooke wrote:
    > Errr... no. And if you think it is, you /really/ don't understand how a
    > Servlet engine works.


    The servlet engine may well re-use the same instance of a servlet class to
    handle multiple requests. The instance refers to its own private variables
    without use of accessor methods. Since multiple threads may be using the
    instance, problems might ensue.

    Synchronizing access to a shared resource involves synchronizing on the
    resource or one that is tied to it, as "synchronized(this)" or
    "synchronized(x)". Synchronizing to the accessor methods is not reliable, as
    it ignores accesses that bypass those methods.

    It's better to stick with stack variables for servlet action.

    - Lew
     
    Lew, Nov 12, 2006
    #10
    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. circuit_breaker
    Replies:
    2
    Views:
    2,061
    Jack Jia
    Apr 4, 2004
  2. Andy Chang

    Is pass by reference thread safe?

    Andy Chang, Mar 2, 2005, in forum: C++
    Replies:
    9
    Views:
    588
    E. Robert Tisdale
    Mar 2, 2005
  3. anita
    Replies:
    7
    Views:
    2,596
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 1, 2007
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,376
    Gabriel Rossetti
    Aug 29, 2008
  5. John Nagle
    Replies:
    5
    Views:
    497
    John Nagle
    Mar 12, 2012
Loading...

Share This Page