Volatile in java

concurrency, java, volatile

Solution

You see the value 0 because the read is executed before the write. And you see the value 10 because the write is executed before the read.

If you want to have a test with more unpredictable output, you should have both of your threads await a CountDownLatch, to make them start concurrently:

final CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            latch.await();
            t.a = 10;
        }
        catch (InterruptedException e) {
            // end the thread
        }
    }
 }).start();
 new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            latch.await();
            System.out.println("Value " + t.a);
        }
        catch (InterruptedException e) {
            // end the thread
        }
    }
 }).start();
 Thread.sleep(321); // go
 latch.countDown();

Problem

As far as I know volatile write happens-before volatile read, so we always will see the freshest data in volatile variable. My question basically concerns the term happens-before and where does it take place? I wrote a piece of code to clarify my question. ``` class Test { volatile int a; public static void main(String ... args) { final Test t = new Test(); new Thread(new Runnable(){ @Override public void run() { Thread.sleep(3000); t.a = 10; } }).start(); new Thread(new Runnable(){ @Override public void run() { System.out.println("Value " + t.a); } }).start(); } } ``` (try catch block is omitted for clarity) In this case I always see the value 0 to be printed on console. Without `Thread.sleep(3000);` i always see value 10. Is this a case of happens-before relationship or it prints 'value 10' because thread 1 starts a bit earlier thread 2? It would be great to see the example where the behaviour of code with and without volatile variable differs in every program start, because the result of code above depends only(at least in my case) on the order of threads and on thread sleeping.

Original source

Related problems