Search code examples
javamultithreadingvariablesvolatilejava-threads

Volatile variable in multithreading program in java


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;
                }
            }
        }
    }
}

Solution

  • 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.