Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

servlet reference and thread safe

Thread view: 
gk - 10 Nov 2006 23:21 GMT
"...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 ?
hiwa - 10 Nov 2006 23:28 GMT
> "...the instance , static   variables used in a servlet reference
> object are not thread safe..."
[quoted text clipped - 21 lines]
>
> 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.
gk - 10 Nov 2006 23:46 GMT
> > "...the instance , static   variables used in a servlet reference
> > object are not thread safe..."
[quoted text clipped - 26 lines]
> 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 ?
hiwa - 11 Nov 2006 00:03 GMT
> > > "...the instance , static   variables used in a servlet reference
> > > object are not thread safe..."
[quoted text clipped - 29 lines]
> if i make synchronized setter/getter methods to access those instance
> variables , will that be thread safe now ?
Yes.
Simon Brooke - 11 Nov 2006 13:52 GMT
>> > > "...the instance , static   variables used in a servlet reference
>> > > object are not thread safe..."
[quoted text clipped - 30 lines]
>> 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.

Signature

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

                       ;; Quidquid latine dictum sit, altum sonatur.

Lew - 12 Nov 2006 15:18 GMT
>>>>> "...the instance , static   variables used in a servlet reference
>>>>> object are not thread safe..."
[quoted text clipped - 6 lines]
>>>>> 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.

> 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
Daniel Pitts - 11 Nov 2006 01:33 GMT
> > > "...the instance , static   variables used in a servlet reference
> > > object are not thread safe..."
[quoted text clipped - 29 lines]
> 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.
Mark Jeffcoat - 11 Nov 2006 09:45 GMT
> 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.

Signature

Mark Jeffcoat
Austin, TX

Simon Brooke - 11 Nov 2006 13:51 GMT
>> 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.

Signature

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

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

Simon Brooke - 11 Nov 2006 13:46 GMT
>  "...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.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
               Ring of great evil
               Small one casts it into flame
               Bringing rise of Men                    ;; gonzoron



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.