显示 Java 中 volatile 和普通变量的区别 [英] Display difference between volatile and usual variable in Java

查看:24
本文介绍了显示 Java 中 volatile 和普通变量的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个示例来显示 volatile 变量和普通变量之间的区别,例如:

I am trying to create an example to display the difference between volatile and usual variables like:

package main;

public class TestVolatile extends Thread {

    public int l = 5;
    public volatile int m = -1;

    public TestVolatile(String str) {
        super(str);
    }

    public void run() {
        int i = 0;
        while ((l > 1) && (l < 10)) {
            if (m >= 0) {
                m++;
            }
            i++;
            l = 5;
            System.out.println("5=" + i + " m=" + m);
        }

    }

    public static void main(String[] args) throws InterruptedException {
        TestVolatile tva = new TestVolatile("ThreadA");

        tva.start();
        sleep(5);
        synchronized (tva) {
            tva.m = 5;
            tva.l = 10;
        }
    }

}

所以 m 是可变的,l 不是.我想退出 while 循环取决于 l 的值.
因为 l 的值不是 volatile - m 将在 l 被分配 5 后至少增加 1 次.但我已经运行了编码 10 次并且总是 m==5.
所以我想我错了.如何解决这个问题?谢谢.

So m is volatile, l is not. I suppose that exiting from the while loop depends on the value of l.
Because the value of l is not volatile - m will be incremented at least 1 time after l has been assigned 5. But I have run the code 10 times and always m==5.
So I suppose that I am wrong. How to fix this problem? Thank you.

感谢您的回答,但并非都运行良好.我设置为:

Thanks for answers, but not all run well. I set like:

volatile int x = 0;
volatile int y = 0;

所以现在变量必须相同!但事实并非如此.

So now the variables have to be the same! But that is not the case.

x: 346946234 y: 346946250
x: 346946418 y: 346946422
x: 346946579 y: 346946582
x: 346946742 y: 346946745
x: 346946911 y: 346946912

推荐答案

您正在同步主线程和测试线程.因此,Java 保证使其他线程执行的任何更改可见.

You are synchronizing the main thread and your test thread. Therefore Java guarantees to make any changes visible performed by the other thread.

顺便说一句,不可能构建一个确定性地显示易失性和非易失性之间差异的示例.您所能希望的最好结果是获得一个程序,该程序以相当高的概率显示差异.如果线程在同一个核心上交错运行.你将无法表现出任何不同.

Btw, it is impossible to construct an example which deterministically shows a difference between volatile and non-volatile. The best you can hope is to get a program which shows the difference with a quite high probability. If the threads run interleaved on the same core. You won't be able to show any difference at all.

以下程序在我的计算机上显示了易失性和非易失性变量之间的区别.

The following program shows on my computer the difference between volatile and non-volatile variables.

public class ShowVolatile {

    final static int NUM_THREADS = 1;

    int x = 0;
    volatile int y = 0;

    public static void main(String... args) {

        final ShowVolatile sv = new ShowVolatile();

        for (int i=0; i< NUM_THREADS; i++) {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        sv.x += 1;    
                        sv.y += 1;    
                    }
                }
            }).start();
        }


        while (true) {
            System.out.println("x: " + sv.x + " y: " + sv.y);
        }
    }

}

如果增加线程数,您将看到更多的同步未命中.但是线程数为 1 就足够了.至少在我的硬件上是四核 i7.

If you increase the number of threads you will see additional synchronization misses. But a thread count of 1 is enough. At least on my hardware a Quad-Core i7.

这篇关于显示 Java 中 volatile 和普通变量的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆