Declaring an object as volatile

If you declare a member variable as volatile in Java, does this mean that all object 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 its instance as follows:

private volatile C obj;

whether the reference to the stores objin volatile memory or objdata ( obj.iand obj.c) in the memory volatile?

Does flow obj.cand obj.ithread safety provide or not?

+5
source share
4 answers

JVM, , . , - volatile, , , .

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

Re: , , , . @gerrytan Oracle, volatile , , , . ...

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

- , , , , obj.doSomething(), obj = null. , synchronized.

+8
private volatile C obj;

obj volatile.

obj.c obj.i ?

. , .

+3

, . obj.i obj.c ,

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

- Java, , ?

, , volatile Object. , CPU, Heap , . , , , , .

obj.c obj.i ? . , , . , , , , - , , .

+1

All Articles