Volatile vs Static in Java
Is it correct to say that static
means one copy of the value for all objects and volatile
means one copy of the value for all threads?
Anyway a static
variable value is also going to be one value for all threads, then why should we go for volatile
?
Declaring a static variable in Java, means that there will be only one copy, no matter how many objects of the class are created. The variable will be accessible even with no Objects
created at all. However, threads may have locally cached values of it.
When a variable is volatile and not static, there will be one variable for each Object
. So, on the surface it seems there is no difference from a normal variable but totally different from static. However, even with Object
fields, a thread may cache a variable value locally.
This means that if two threads update a variable of the same Object concurrently, and the variable is not declared volatile, there could be a case in which one of the thread has in cache an old value.
Even if you access a static value through multiple threads, each thread can have its local cached copy! To avoid this you can declare the variable as static volatile and this will force the thread to read each time the global value.
However, volatile is not a substitute for proper synchronisation!
For instance:
private static volatile int counter = 0;
private void concurrentMethodWrong() {
counter = counter + 5;
//do something
counter = counter - 5;
}
Executing concurrentMethodWrong
concurrently many times may lead to a final value of counter different from zero!
To solve the problem, you have to implement a lock:
private static final Object counterLock = new Object();
private static volatile int counter = 0;
private void concurrentMethodRight() {
synchronized (counterLock) {
counter = counter + 5;
}
//do something
synchronized (counterLock) {
counter = counter - 5;
}
}
Or use the AtomicInteger
class.
Difference Between Static and Volatile :
Static Variable: If two Threads(suppose t1
and t2
) are accessing the same object and updating a variable which is declared as static then it means t1
and t2
can make their own local copy of the same object(including static variables) in their respective cache, so update made by t1
to the static variable in its local cache wont reflect in the static variable for t2
cache .
Static variables are used in the context of Object where update made by one object would reflect in all the other objects of the same class but not in the context of Thread where update of one thread to the static variable will reflect the changes immediately to all the threads (in their local cache).
Volatile variable: If two Threads(suppose t1
and t2
) are accessing the same object and updating a variable which is declared as volatile then it means t1
and t2
can make their own local cache of the Object except the variable which is declared as a volatile . So the volatile variable will have only one main copy which will be updated by different threads and update made by one thread to the volatile variable will immediately reflect to the other Thread.
In addition to other answers, I would like to add one image for it(pic makes easy to understand)
static
variables may be cached for individual threads. In multi-threaded environment if one thread modifies its cached data, that may not reflect for other threads as they have a copy of it.
volatile
declaration makes sure that threads won't cache the data and uses the shared copy only.
image source
I think static
and volatile
have no relation at all. I suggest you read java tutorial to understand Atomic Access, and why use atomic access, understand what is interleaved, you will find answer.