wood burning stoves 2.0*
The moose likes Servlets and the fly likes Thread safe variable declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread safe variable declarations" Watch "Thread safe variable declarations" New topic
Author

Thread safe variable declarations

Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hi ranchers!
I have trouble understanding whether this is thread safe or not...and I hope I'm posting this in the right forum. In some classes or servlets I find it easier to declare variables without instantiating them so I can use them within any method in the class. By doing this though, I worry that if 2 simultaneous connections occur that they may both be sharing the same variable. Here is an example of what I mean:

Whether the above example is a servlet or a helper class and if thread safety is paramount...is this dangerous? Talking to some programmer friends I get conflicting answers on this so I really would like a clear definition. Even in the books I have on Java this is clearly not defined.
Thanks for any insight on this!
-Pat
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi,
I think this forum is strickly for beginners and there is a forum named Servlets, you should have posted your query in that forum.
Taken from Java in General(beginner) home page:
BEWARE: Persons caught posting questions of any difficulty in this forum are subject to fines of up to 10 post responses. Only responses that make SENSE will be counted towards the remittance of said fine.

so beware.


Thanks,<p>Vijay<p>The Hand that gives, Gathers.
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
Considering thread safety this piece of code is not safe at all and should be avoided whenever is possible. Any subsequent http request received by this servlet will overwrite class variables with new values while code in doStuff might still be executing.
Use synchronization or do not use class variables - pass parameters to methods, or use SingleThreadModel servlets.
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
ugh...I did not want to hear this. I took over a project that does this in several classes. Most of them are for admin screens so there is little worry of 2 people accessing them at the same time...but there are a couple of classes utilized by the public that do this as well.
If this is absolutely certain...then I guess I have to rewrite these pages so all variables are declared within methods, as I do not want to use synchronization. DOH! *shaps forehead*
Oh, and sorry if I did post in the wrong forums. 10 sensible replies coming up
-Pat
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hey Ranchers,
I just got thinking about my above question again and I'm still having trouble understanding this. If I use a bean like this:

my understanding is that foo is an instance variable and is Thread safe. If that's correct, why is my above example not?
Thanks in advance...I really want to get a firm understanding on this issue!
-Pat
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pat Wallwork:
If I use a bean like this:

my understanding is that foo is an instance variable and is Thread safe. If that's correct, why is my above example not?

Access to foo is not thread-safe when different threads are working on the same instance of Bean.
That is what is happening to your servlet - one servlet instance might handle several doGet calls at the same time. You can restrict the servlet to one call per instance by implementing javax.servlet.SingleThreadModel (see http://java.sun.com/products/servlet/2.1/api/javax.servlet.SingleThreadModel.html ).
Did that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You bean class is encapsulated, but that does not imply thread safety.

If you don't understand *why* it's not thread safe, here's a scenario:

Thread A calls setFoo(5)
Thread A calls getFoo()

Thread B calls setFoo(10)
Thread B calls getFoo()


Thread A would expect 5 and Thread B would expect 10, right?

But what if Thread B decided to get impatient?

Thread A calls setFoo(5)
Thread B calls setFoo(10)

Thread B calls getFoo()
Thread A calls getFoo()


Now Thread A will get an unexpected result.. it will get 10 along with Thread B getting 10. What's worse... let's say these are not simple getters and setters, and that after the second call by Thread B, some object is now nulled. Suddenly Thread A will throw a NullPointerException.

Lots of bad stuff happens to programs that are not threadsafe.
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Wow...then I really do have to take a look over all my previous apps to ensure thread safety! Most cases will likely be ok as they are beans stored in the users session, but in the future I think I'll stick to declaring variables in methods only then!
Thanks for all the info, I'm glad I learned this now before disaster struck!
-Pat
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread safe variable declarations
 
Similar Threads
Is static inner class thread safe?
Is static inner class thread safe?
Is this thread safe ?
how are local variables in doPost() thread safe?
Is static inner class thread safe?