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
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.
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.
Joined: Sep 23, 2001
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
Joined: Sep 23, 2001
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
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
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.
Joined: Sep 23, 2001
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