-1

I am reading an article about the Java Volatile keyword, got some questions. click here

public class SharedObject {

    public int counter = 0;

}

Imagine too, that only Thread 1 increments the counter variable, but both Thread 1 and Thread 2 may read the counter variable from time to time.

If the counter variable is not declared volatile there is no guarantee about when the value of the counter variable is written from the CPU registers back to main memory. This means, that the counter variable value in the CPU register may not be the same as in main memory.

How to verify that visibility cannot be guaranteed without volatile?

I have tried creating two threads A and B, where A executes counter=1 and asserts counter==1 in thread B. Counter is not decorated with volatile. However, asserts in thread B always succeed If the assert of thread B fails, it means that counter==0 is a visibility issue, but this has not happened If this is not easy to prove, do I need to use volatile to ensure visibility when writing a program where one thread writes to another thread for reading?

9
  • You prove it by applying the Java Memory Model rules (JLS 17.4) to your program. The JMM states precisely when visibility is guaranteed. You can't "prove" that visibility is guaranteed by writing and running code. Just because you observe that your code sees the value doesn't mean that it will always see it ... on all runs, all compilers, all platforms, etc.
    – Stephen C
    Commented Oct 9, 2024 at 4:53
  • If you're asking how to demonstrate it, have thread 2 loop while (counter == 0) and it'll probably loop infinitely. Just make sure not to print inside the loop.
    – shmosel
    Commented Oct 9, 2024 at 4:54
  • As @shmosel alludes ... there are all sorts of "environmental effects" that can alter the visibility characteristics of code that doesn't satisfy the requirements for guaranteed visibility. If you manage to find a demo where the behavior is explained (only) by non-visibility, you have probably proved that visibility is not guaranteed. But proving that visibility is guaranteed with a demo is not possible.
    – Stephen C
    Commented Oct 9, 2024 at 5:00
  • 1
    "How to verify that visibility cannot be guaranteed without volatile?" - And ... in fact, that is not even a correct statement. Visibility can be guaranteed other ways. For example, you can use synchronized or a higher level Java concurrency class that has specified HB characteristics. Obviously you need to change your example code to do that ...
    – Stephen C
    Commented Oct 9, 2024 at 5:08
  • 1
    If you really want to understand this stuff, you should start by getting hold of a copy of Goetz et al - "Java Concurrency in Practice".
    – Stephen C
    Commented Oct 9, 2024 at 5:09

1 Answer 1

-1

To make sure volatile works, you can disable JIT compilation with the -Xint parameter and use this code:

public class VolatilityDemo {

    private static volatile boolean value = true;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            while (value);
        }).start();
        Thread.sleep(1000);
        value = false;
    }
}

If you remove volatile, the thread will never terminate and the application will hang.

2
  • loop while (value),I think this is caused by JIT reordering.value has been reordered outside of 'while',You can disable the reordering of JIT by using the setOpaque (value) method of varHandler in JDK9+.This is not an issue caused by visibility
    – qwee
    Commented Oct 9, 2024 at 5:27
  • @qwee I suggest you just disable JIT compilation with the -Xint parameter. Commented Oct 9, 2024 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.