Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need volatile for variables of reference types, too?

We often use volatile to ensure that a condition variable can be visible to every Thread.

I see the volatile fields are all primitive type in code so far.

Does object field has this problem? For example:

class a {     public String str;     public List list;  } 

If there are some threads which will access str and list, must I add 'volatile'?

I guess each access to Object will get directly from Heap, and the Object will not be cached like primitive type.

Is that right?

like image 951
Hesey Avatar asked Aug 16 '11 08:08

Hesey


People also ask

When should volatile be used?

Yes, volatile must be used whenever you want a mutable variable to be accessed by multiple threads. It is not very common usecase because typically you need to perform more than a single atomic operation (e.g. check the variable state before modifying it), in which case you would use a synchronized block instead.

When should I use volatile in C?

Volatile is used in C programming when we need to go and read the value stored by the pointer at the address pointed by the pointer. If you need to change anything in your code that is out of compiler reach you can use this volatile keyword before the variable for which you want to change the value.

What is the purpose of a volatile variable?

The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory. Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory.

Which variables are volatile?

A volatile variable is a variable that is marked or cast with the keyword "volatile" so that it is established that the variable can be changed by some outside factor, such as the operating system or other software.

Are volatile variables always visible to other threads?

The volatile variables are always visible to other threads. The volatile variable that is an object reference may be null. When a variable is not shared between multiple threads, you do not need to use the volatile keyword with that variable.

What happens if we do not use volatile variable in C++?

If you do not use volatile variable compiler can reorder the code, free to write in cache value of volatile variable instead of reading from the main memory. You can use the volatile keyword with variables. Using volatile keyword with classes and methods is illegal.

What is the purpose of using volatile variables?

There are two main reasons to uses volatile variables: To interface with hardware that has memory-mapped I/O registers. When using variables that are modified outside the program control flow (e.g., in an interrupt service routine) Let's see this example:

Can a volatile variable be null in Java?

The volatile variable that is an object reference may be null. When a variable is not shared between multiple threads, you do not need to use the volatile keyword with that variable. Volatile keyword is not a substitute of synchronized keyword, but it can be used as an alternative in certain cases.


2 Answers

You have to distinguish between the object reference and the actual object.

  • For the reference your field modifier is relevant. When you change the reference to a different object (i.e. reference a different String) the change might not be noticed by a different Thread. If you want to enforce visibility you have to use final or volatile.

  • The actual object on the heap is not affected by the field modifier. Instead how you see each field of this object is determined by its own field modifier according to the same rules (is it volatile or final? If not, visibility for concurrent Threads is not enforced)

So the answer is: Yes, you have to add volatile or final. Stylistically it would be much better to make the field final, though. It has the same effect Thread-wise but is a stronger statement, too: This field cannot be changed - which is the reason why it can be cached heedlessly by the JVM. And for the same reason it comes with a little performance benefit compared to volatile, Java needs not care whether the field is changes again and does not need to add overhead.

like image 189
Stefan Schubert-Peters Avatar answered Oct 01 '22 10:10

Stefan Schubert-Peters


You add volatile keyword to tell the compiler that its bound to change. so that all threads re-confirm that their local cached copy is same as that of original one, if there are changes it updates it. and you find it usually with Primitives because usually primitive values are incremented or decremented etc by different threads. but Object, especially List will never change since its just a reference to the Object. if you are assigning different objects to the same variable, at run-time then you may do..

like image 27
ngesh Avatar answered Oct 01 '22 12:10

ngesh