aspose file tools
The moose likes Threads and Synchronization and the fly likes Can Static Variables have multiple copies when in a multi threaded environment and under heavy load? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Can Static Variables have multiple copies when in a multi threaded environment and under heavy load?" Watch "Can Static Variables have multiple copies when in a multi threaded environment and under heavy load?" New topic
Author

Can Static Variables have multiple copies when in a multi threaded environment and under heavy load?

Prafull Prashant
Greenhorn

Joined: Jan 15, 2010
Posts: 8
Hi All,

I am writing a code which runs in a mult- threaded environment.

I have declared a static variable which is globally visible across the application. Each thread landing on a piece of code has the capability to change the value of my static variable.

My question is, under heavy load condition, is it possible that at the same time two different threads may have different values of the static variable (assuming at the time two threads are looking up there is no change in value of that variable by other threads).

The question arose when someone pointed me that at runtime JVM optimizes the static variable references by caching it in locally and can lead to multiple values of same variable. Hence, its never use static variable, instead use volatile variable.

Is this true? Please let me know if i was able to describe my problem properly


Prafull

SCJP
Kaustav Ganguly
Greenhorn

Joined: Dec 14, 2009
Posts: 28
Yes, it is required to make the variable volatile to make it thread safe.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can Static Variables have multiple copies when in a multi threaded environment and under heavy load?
 
Similar Threads
Multiple Home & env. entities
Threads 002
Need to understand about threading issues?
volatile and static keywords
How do you execute new code in an existing Thread?