Volatile variable and non volatile reordering / visibility

JJ Vester

So I thought I knew this stuff well enough, until I read something which got me doubting my knowledge on this subject matter. I am almost certain the book is incorrect but would like to ask the community as well.

PS: Have not seen the errata of the book so could well be disclosed as an error.

A simplified example:

public class VolatileMain {

private volatile int a = 0;
private String text = "";

public static void main(String[] args) throws Exception {

    VolatileMain vm = new VolatileMain();

    Thread writer = new Thread() {

        @Override
        public void run() {
            System.out.println("Running thread " + Thread.currentThread().getName());
            vm.text = "hello world";
            vm.a = 5;
        }
    };

    writer.start();
    writer.join();

    System.out.println("Running thread " + Thread.currentThread().getName());
    System.out.println(vm.a);
    System.out.println(vm.text);

   }

}

So given the example is it correct to assume that the write to "text" by Thread writer is guaranteed to be visible by any other thread that reads it?

It seems the author is piggy backing on the volatile semantics of the variable "a" and ensuring that the write to "text" will also be flushed when "a" is flushed, is this a guarantee?

I didn't think it was, but my own quick test (above) to the contrary

Your thoughts.

JB Nizet

is it correct to assume that the write to "text" by Thread writer is guaranteed to be visible by any other thread that reads it?

No. But it's guaranteed to be visible by any other thread that reads a before reading text, as your example does:

  • the write of text happens-before the write to a in the writer thread
  • the write of a in writer happens-before the read of a in the main thread
  • the happens-before relation is transitive
  • hence the the write of text happens before the read of a.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Volatile variable and non volatile reordering / visibility

From Dev

Can a reordering happen in the presence of a volatile variable?

From Dev

Do Android ART and HotSpot behave differently in non-volatile variable visibility?

From Dev

Full volatile Visibility Guarantee

From Dev

Volatile Pointer to Non Volatile Data

From Dev

Understanding atomic-ness, visibility and reordering of synchonized blocks vs volatile variables in Java

From Dev

Volatile array - memory visibility of the elements

From Dev

memory visibility without synchronized or volatile

From Dev

Will the Java Memory Model permit the reordering of non-synchronized accesses of many atomic/volatile variables?

From Dev

How to understand volatile and non-volatile registers?

From Java

Cast volatile array to non volatile array

From Dev

java volatile objects in non volatile objects

From Dev

Understanding volatile variable by example

From Dev

Force read of volatile variable

From Java

java - synchronization and volatile variable

From Dev

Java Volatile Variable

From Dev

Order of const and volatile for a variable

From Dev

Incrementing a volatile variable in C

From Dev

Partially volatile variable?

From Dev

Casting volatile variable in c

From Dev

Partially volatile variable?

From Java

Why non volatile variable is updated on CPU shared cache?

From Dev

visibility difference between synchronization of field reads and volatile

From Dev

visibility difference between synchronization of field reads and volatile

From Dev

Volatile variable explanation in Java docs

From Dev

How can an auto variable be volatile?

From Dev

Combination of Singleton class and volatile variable

From Dev

When not to use volatile variable in Java

From Dev

Java volatile variable multithreading behavior

Related Related

  1. 1

    Volatile variable and non volatile reordering / visibility

  2. 2

    Can a reordering happen in the presence of a volatile variable?

  3. 3

    Do Android ART and HotSpot behave differently in non-volatile variable visibility?

  4. 4

    Full volatile Visibility Guarantee

  5. 5

    Volatile Pointer to Non Volatile Data

  6. 6

    Understanding atomic-ness, visibility and reordering of synchonized blocks vs volatile variables in Java

  7. 7

    Volatile array - memory visibility of the elements

  8. 8

    memory visibility without synchronized or volatile

  9. 9

    Will the Java Memory Model permit the reordering of non-synchronized accesses of many atomic/volatile variables?

  10. 10

    How to understand volatile and non-volatile registers?

  11. 11

    Cast volatile array to non volatile array

  12. 12

    java volatile objects in non volatile objects

  13. 13

    Understanding volatile variable by example

  14. 14

    Force read of volatile variable

  15. 15

    java - synchronization and volatile variable

  16. 16

    Java Volatile Variable

  17. 17

    Order of const and volatile for a variable

  18. 18

    Incrementing a volatile variable in C

  19. 19

    Partially volatile variable?

  20. 20

    Casting volatile variable in c

  21. 21

    Partially volatile variable?

  22. 22

    Why non volatile variable is updated on CPU shared cache?

  23. 23

    visibility difference between synchronization of field reads and volatile

  24. 24

    visibility difference between synchronization of field reads and volatile

  25. 25

    Volatile variable explanation in Java docs

  26. 26

    How can an auto variable be volatile?

  27. 27

    Combination of Singleton class and volatile variable

  28. 28

    When not to use volatile variable in Java

  29. 29

    Java volatile variable multithreading behavior

HotTag

Archive