• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

threads and static varibles

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all the advices. I ended up making it for the session.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic