user965369
user965369

Reputation: 5733

Declaring an object as volatile

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?

Upvotes: 18

Views: 16515

Answers (4)

ravinn
ravinn

Reputation: 21

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.

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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.

Upvotes: 5

Brad
Brad

Reputation: 15879

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.

Upvotes: 31

Amar
Amar

Reputation: 12010

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';
}

Upvotes: 3

Related Questions