0

Why do primitive variable in multithreading little program, behave as a volatile variable? Please help me in my code.

/**
 * Practice with threads problem visibility.
 * @author Matevosyan Vardan
 * @version 1.0
 * created on 21.09.2017
 */

public class VisibilityProblem {
    private static int countingVal = 0;

    public static int getCountingVal() {
        return countingVal;
    }

Start in main

    public static void main(String[] args) throws InterruptedException {
       Thread looperR = new VisibilityProblem.Looper();
        Thread listener = new VisibilityProblem.Listener();

        listener.start();
        looperR.start();

        listener.join();
        looperR.join();
    }

Class to wright and increase counting variable after sleep 500 millisecond to wait a little, what helps do some staff Listener thread.

    public static class Looper extends Thread {
        @Override
        public void run() {
            while (VisibilityProblem.countingVal < 5) {
                VisibilityProblem.countingVal++;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("increase " + VisibilityProblem.countingVal);
            }
        }
    }

Class to read and assign counting value

    public static class Listener extends Thread {
        @Override
        public void run() {
            int localCount = VisibilityProblem.countingVal;
            while (localCount < 5) {
                if (localCount != VisibilityProblem.countingVal) {
                    System.out.println("It is " + localCount + " now");
                    localCount = VisibilityProblem.countingVal;
                }
            }
        }
    }
}

1 Answer 1

2

Why do primitive variable in multithreading little program, behave as a volatile variable

It depends what you mean by behave as a volatile variable.

  • If you mean, why are changes made by one thread seen in the second thread ... then the reason is that the JLS allows this.

  • If you mean, why are changes made by by one thread guaranteed to be seen in the second thread ... then your program does not provide evidence1 of this!!

The difference in visibility semantics between ordinary and volatile variables are:

  • For a volatile, the changes are guaranteed to be immediately visible.

  • For an ordinary variable, the changes may be visible immediately, or with a delay, or ... never. Any of those behaviors conforms with the Java Memory Model.

Seeing the changes with ordinary variables when you run the program on one hardware platform with one version of the Java compiler on a computer running with one set of applications running, etc, etc does not mean that you will always see that in all circumstances.


1 - Indeed, it is theoretically impossible to write a program that can do this. But your program could provide evidence that this is not guaranteed ... or that a (hypothetical) guarantee is not being met.

Sign up to request clarification or add additional context in comments.

5 Comments

For a non-volatile variable, changes may only never be visible if not followed by a synchronization action ... ever.
I am aware of that. However, the question is about the difference in volatile and ordinary variables, and how to write a program that reliably demonstrates the difference.
I understand. My comment is in the vein of: "never" is only ever conforming with the JMM in this specific case. As is, someone that reads that sentence may believe it's OK for changes on non-volatile fields to never be visible no matter the case. If you find this is not relevant for the answer, it ought to not be there in the first place, or replace "never" with "up to the next synchronization action, which may never happen".
"As is, someone that reads that sentence may believe it's OK for changes on non-volatile fields to never be visible no matter the case." - Well it is OK according to the JLS for changes to never be visible. So if people believe that, then they are absolutely correct. And that is exactly the point I am trying to make. Please note my careful and advised use of the word "may" ...
In short ... what you say is correct, but it detracts from the point I am trying to make. (And it is not relevant to the question ... because in the question there is no overt synchronizing action!)

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.