关于引用类型的 Java volatile 关键字的问题

发布于 2024-11-13 04:44:57 字数 616 浏览 2 评论 0原文

我理解Java中的 volatile 关键字可以使引用变量和除 long 和 double 之外的所有原语的读/写操作本质上是原子的。

我还知道复合语句(例如递增整数 var++)不是原子的,不应该用来代替同步语句。

但是这堂课呢?

public class Setter{

    private int num = 0;

    public int setNum(int numIn){
        num = numIn;
        return num;
    }
}

现在假设您将 Setter 的实例声明为易失性的。

public class Main {
     private volatile Setter s;

     public static void main(String[] args){
         s = new Setter();
         s.setNum(5);
     }
}

对 setNum 方法的调用是原子的吗?如果多个线程同时调用该语句且每个线程具有不同的值,该语句是否安全?

以正确的方式使用易失性类并在不安全的复合语句中使用它的示例是什么?

I understand the volatile keyword in Java can make the read/write operations of reference variables and all primitives except long and double, atomic in nature.

I also know compound statements such as incrementing an integer, var++, are not atomic and should not be used in place of synchronized statements.

But what about this class?

public class Setter{

    private int num = 0;

    public int setNum(int numIn){
        num = numIn;
        return num;
    }
}

Now say you declare an instance of Setter as volatile.

public class Main {
     private volatile Setter s;

     public static void main(String[] args){
         s = new Setter();
         s.setNum(5);
     }
}

Is a call to the method setNum atomic? Is this statement safe if multiple threads are calling it at once, each with different values?

What would be an example of using a volatile class in a proper way, and using it in an unsafe compound statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-11-20 04:44:57

对 setNum 方法的调用是原子的吗?

不,不是。只有对 s 的读/写才是易失性的。

这可以与让 Listfinal 进行比较。这不足以使列表不可变,仅使列表引用本身不可变。

Is a call to the method setNum atomic?

No, it's not. It is only the reads / writes to s that would be volatile.

This can be compared with letting a List be final. This is not sufficient to make the list immutable, only the list-reference itself.

扶醉桌前 2024-11-20 04:44:57

不,易失性的使用仅适用于引用s,而不适用于它所引用的对象的字段。您不能(安全地)让多个线程调用 s.setNum(n)

但是,您可以让线程创建一个新的 Setter 对象,在其中存储一个数字,然后更改 s 以引用新对象而不是旧对象。

No, that use of volatile only applies to the reference s, not the fields of the object it refers to. You can't (safely) have multiple threads call s.setNum(n).

You could, however, have a thread create a new Setter object, store a number into it, and then change s to refer to the new object instead of the old one.

胡渣熟男 2024-11-20 04:44:57

对对象的引用和该对象的内容是不同的东西。在您的情况下,对象的引用(是您的 Main 类的 prat)是 易失性,这意味着当您读/写引用易失性规则将适用。您可以自由地对同一个非易失性对象进行另一个引用,并且易失性规则将不适用于它。

The reference to an object and the content of that object are different things. In your case, the reference (that is prat of your Main class) to the object is volatile, meaning that when you read/write the reference the volatile rules will apply. You can freely have another reference to the same object that is not volatile and volatile rules wont apply to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文