• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads and static varibles  RSS feed

 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some static variables in a class. When the http request comes in it sets some values of the static variables and based on these static variables a list is created using values from the database. Each request comes in with different values. I guess when each request comes in it runs in a different thread. Is the values static variables used by each thread different, or the value set by the second request is used by the first thread as well? I appreciate your help.
Thank you very much in advance.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables, static or instance, are not thread safe. Unless you explicitly protect them (lock, sync or atomic) then each thread will modify them when they run.

For static variables, you can always sync on the class object:
synchronized(MyClass.class)
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the values static variables used by each thread different

static variables are shared between all *objects* may or may not be in different threads.
If there are two different instances of the same class in two different thread and you assume the value of a static variable to be constant in one thread execution, it is a recipie for disaster
In this case you would have to *protect* the access of the static variables by locks.
[ May 09, 2007: Message edited by: Nitesh Kant ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making the access synchronized would only guarantee that only one thread is accessing that value at a time.

But if you need this value to be only available to one thread then each executing thread should have one exclusive copy of this.Making it static (off course public) would still exposes it to all.

Why is your application setting this value to a static variable.It can set it to some place which is exclusive for that request or may be session.
 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the advices. I ended up making it for the session.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the session is indeed often a good thing in Web apps. Whether it is correct in your situation depends whether the thing you're storing really is one-per-session or one-per-thread. If it's the latter, then using the session is not really correct, and could go wrong.

There is a class ThreadLocal that can be used to get independent values of a variable for each thread. Check it out in the API.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edward]: Variables, static or instance, are not thread safe.

I would rephrase this. Static and instance variables are not thread-safe (unless you protect them). Local variables (and parameters) are always thread-safe.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!