Declaring an object as volatile
Asked Answered
P

4

18

If you declare a member variable as volatile in Java, does this mean that all the object's data is stored in volatile memory, or that the reference to the object is stored in volatile memory?

For example if I have the following class:

class C{
   int i = 0;
   char c = 'c';
}

If I declare an instance of it as follows:

private volatile C obj;

does that store the reference to obj in volatile memory, or obj's data (obj.i and obj.c) in volatile memory?

Does it make obj.c and obj.i thread safe or not?

Plutonium answered 18/3, 2013 at 21:45 Comment(3)
Java SE 6 tutorial doesn't specify volatile keyword as storing object in volatile memory, but it ensure read and writes are atomic. docs.oracle.com/javase/tutorial/essential/concurrency/…Recumbent
All variables are stored in volatile memory (i.e. RAM), regardless of the volatile modifier. en.wikipedia.org/wiki/Volatile_memoryPieria
@Recumbent volatile is not so much about atomicity, it is about visibility.Cowry
P
30

Yes only the object reference will be considered to be volatile by the JVM and not the object data itself which will reside on the heap. If you required the member variables of the object on the heap to be volatile you can of course apply the keyword to those primitives

class C {
   volatile int i = 0;
   volatile char c = 'c';
}

Re: your question of whether this makes the variable thread safe, depends on how you are using the variable. As @gerrytan pointed out from the Oracle docs, the volatile keyword does help with a read or write to be atomic, however be warned that this is not the same as it always being thread safe. Consider the following code...

if(obj != null) {
    obj.doSomething();
}

It is still possible that a thread that executes the null check, is interrupted before it executes obj.doSomething(), and another thread sets obj = null. Some other mechanism is required here such as a synchronized block.

Potion answered 18/3, 2013 at 21:54 Comment(0)
R
5
private volatile C obj;

That will make only obj volatile.

Does it make obj.c and obj.i thread safe or not?

No. To make them thread-safe, you have to synchronize the access to them.

Rene answered 18/3, 2013 at 21:49 Comment(1)
This reflects the general principle that instances of a class aren't thread safe or not; classes are thread safe or not.Prerequisite
S
3

This would only make the object reference volatile. In order to make obj.i and obj.c also volatile you have to make them volatile explicitly

class C{
   volatile int i = 0;
   volatile char c = 'c';
}
Seldun answered 18/3, 2013 at 21:50 Comment(0)
D
2

If you declare a member variable as volatile in Java, does this mean that all the object's data is stored in volatile memory, or that the reference to the object is stored in volatile memory?

When we declare the object as volatile we are actually telling the underlying processor how to work with the Volatile Object.Every time it is read by CPU instruction a fresh copy is called from Heap and every time a write is done on the object it is saved to heap and is available to other threads. Thus ,the processor does not picks from cache old value neither it write to cache and waits for cache to be written back to the Heap.

Does it make obj.c and obj.i thread safe or not? No. It just gurantees that you will always have a fresh copy when you will read the object.And it is very well possible that while you using the variable and processing logic based on varilable value someone might have changed the value in background and when you updated it ,you are updating a different value.

Directive answered 14/5, 2013 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.