Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile guarantee safe publication of a mutable object?

By reading Java Concurrency in Practice

I can see:

To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by:

  • Initializing an object reference from a static initializer
  • Storing a reference to it into a volatile field or AtomicReference
  • Storing a reference to it into a final field of a properly constructed object
  • Storing a reference to it into a field that is properly guarded by a lock.

However, I am confused about the second idiom. Since volatile can only guarantee that the reference is visible to another thread but it doesn't have synchronization of object construction which it refers to. So how can it guarantee that the mutable object is properly constructed, what is the thread that is constructing this object is interrupted by another thread?

like image 463
Guifan Li Avatar asked Oct 18 '16 21:10

Guifan Li


3 Answers

We need to show that constructing an object and assigning it to a volatile variable happens before it is read from that variable.

From JLS Chapter 17:

If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).

So, construction of an object happens before it is assigned to a volatile variable, from the point of view of that thread.

If an action x synchronizes-with a following action y, then we also have hb(x, y).

And:

If hb(x, y) and hb(y, z), then hb(x, z).

If we could show that writing the volatile variable (action y) synchronizes-with reading the variable (action z), we could use the transitivity of happens-before to show that constructing the object (action x) happens-before reading the object. Luckily:

A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent reads of v by any thread (where "subsequent" is defined according to the synchronization order).

Therefore, we can see that a properly constructed object is visible to any thread when published this way.

like image 198
erickson Avatar answered Dec 10 '22 17:12

erickson


Reads and writes are atomic for all variables declared volatile (including long and double variables).

Consequence: volatile guarantees, among others, that the variable is always read from the memory shared by all threads - if the value is published in a volatile variable, it must be fully constructed before.
In other words, if a volatile value is not published, then no other threads will know about it - most probably the result of 'construction in progress' may reside in the CPU cache or in the memory that JVM uses as "space I'm using for my own purposes; you puny Java code, don't ask what's in it, it's not your business".

like image 44
Adrian Colomitchi Avatar answered Dec 10 '22 18:12

Adrian Colomitchi


volatile can only guarantee that the reference is visible to another thread but it doesn't have synchronization of object construction which it refers to.

Yes. You are right. You can find more details of internals about volatile variables in below question:

Difference between volatile and synchronized in Java

So how can it guarantee that the mutable object is properly constructed, what is the thread that is constructing this object is interrupted by another thread?

You have to use other programming constructs for thread safety : Using synchronized constructs or alternatives of synchronized constructs.

Refer to below related SE question:

Avoid synchronized(this) in Java?

like image 28
Ravindra babu Avatar answered Dec 10 '22 16:12

Ravindra babu