Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > servlet reference and thread safe

Reply
Thread Tools

servlet reference and thread safe

 
 
gk
Guest
Posts: n/a
 
      11-10-2006
"...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 ?

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      11-10-2006
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.

 
Reply With Quote
 
 
 
 
gk
Guest
Posts: n/a
 
      11-10-2006

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 ?

 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      11-11-2006

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.

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-11-2006

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.

 
Reply With Quote
 
Mark Jeffcoat
Guest
Posts: n/a
 
      11-11-2006
"gk" <(E-Mail Removed)> 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(
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(
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
 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      11-11-2006
in message <(E-Mail Removed) om>, gk
('(E-Mail Removed)') 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
Ring of great evil
Small one casts it into flame
Bringing rise of Men ;; gonzoron

 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      11-11-2006
in message <(E-Mail Removed). com>, gk
('(E-Mail Removed)') 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.

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

;; Life would be much easier if I had the source code.

 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      11-11-2006
in message <(E-Mail Removed) .com>, hiwa
('(E-Mail Removed)') 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.

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

;; Quidquid latine dictum sit, altum sonatur.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-12-2006
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html5lib not thread safe. Is the Python SAX library thread-safe? John Nagle Python 5 03-12-2012 04:07 PM
os.ChDir() not thread-safe; was : Is tempfile.mkdtemp() thread-safe? Gabriel Rossetti Python 0 08-29-2008 08:30 AM
Why no thread.sleep in servlet and a light way to poll from a servlet anita Java 7 09-01-2007 09:22 PM
Is pass by reference thread safe? Andy Chang C++ 9 03-02-2005 03:56 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments