简体   繁体   English

Java volatile关键字行为因删除局部变量而发生变化

[英]Java volatile keyword behaviour changes due to remove local variable

From the link , it provide a demo of the java keyword 'volatile'. 通过该链接 ,它提供了Java关键字“ volatile”的演示。 The demo code works fine. 演示代码可以正常工作。 But I try to do a little modification. 但是我尝试做一些修改。 the behaviour is defferent. 该行为是不同的。

My code : 我的代码:

public class VolatileTest4 {

    private static int MY_INT = 0;

    public static void main(String[] args) {
        new ChangeListener().start();
        new ChangeMaker().start();
    }

    static class ChangeListener extends Thread {
        @Override
        public void run() {
            while (MY_INT < 5) {
                System.out.println("Got Change for MY_INT : " + MY_INT);
            }
        }
    }

    static class ChangeMaker extends Thread {
        @Override
        public void run() {

            while (MY_INT < 5) {
                System.out.println("Incrementing MY_INT to " + MY_INT);
                MY_INT++;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

What I do is just remove the local variable of local_value. 我要做的只是删除local_value的局部变量。 The post says 'Without the volatile keyword, the change listener loop infinitely'. 帖子说“没有volatile关键字,更改侦听器将无限循环”。 But My code is Without the volatile keyword, the change listener ends normally. 但是我的代码没有volatile关键字,更改侦听器正常结束。

What is the defference? 有什么不同? what cause the change listener end? 是什么原因导致更改侦听器结束?

I've compared the code you've provided to the code in the article and there are substantive differences. 我已经将您提供的代码与文章中的代码进行了比较,并且存在实质性差异。 Having modified and run the code from the article in the manner described by the author, I was able to replicate his results. 按照作者描述的方式修改并运行了本文中的代码,我便能够复制他的结果。 My code is as follows: 我的代码如下:

public class VolatileTest {

    private static int MY_INT = 0;

    public static void main(String[] args) {
        new ChangeListener().start();
        new ChangeMaker().start();
    }

    static class ChangeListener extends Thread {
        @Override
        public void run() {
            int local_value = MY_INT;
            while ( local_value < 5){
                if( local_value!= MY_INT){
                    System.out.println(String.format("Got Change for MY_INT : %S", MY_INT));
                    local_value= MY_INT;
                }
            }
        }
    }

    static class ChangeMaker extends Thread{
        @Override
        public void run() {

            int local_value = MY_INT;
            while (MY_INT <5){
                System.out.println(String.format("Incrementing MY_INT to %S", local_value+1));
                MY_INT = ++local_value;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) { e.printStackTrace(); }
            }
        }
    }
}

The volatile keyword changes a variable's visibility semantics. volatile关键字更改变量的可见性语义。 Changes to a member marked volatile become visible to all threads after the write operation completes. 写操作完成后,对标记为volatile的成员的更改将对所有线程可见。 However , the absence of volatile doesn't mean that the changes won't be visible. 但是 ,缺少volatile 并不意味着更改将不可见。 volatile provides some certainty around visibility; volatile为可见volatile提供了一定的确定性; without it, you can't be sure when changes made to a value will become visible in other threads, if ever. 没有它,您将无法确定对值所做的更改何时会在其他线程中可见(如果有的话)。

The author is trying to make the point that because the variable is not marked volatile the changes made by the ChangeMaker are not visible to the ChangeReader and in turn, the ChangeListener never terminates. 作者试图让一点,因为变量没有标记volatile所做出的改变ChangeMaker是不可见的ChangeReader和反过来, ChangeListener永远不会终止。 See this article for a better treatment of the volatile keyword. 请参阅文章为更好的治疗的volatile关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM