关于引用类型的 Java volatile 关键字的问题
我理解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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,不是。只有对
s
的读/写才是易失性的。这可以与让
List
为final
进行比较。这不足以使列表不可变,仅使列表引用本身不可变。No, it's not. It is only the reads / writes to
s
that would be volatile.This can be compared with letting a
List
befinal
. This is not sufficient to make the list immutable, only the list-reference itself.不,
易失性
的使用仅适用于引用s
,而不适用于它所引用的对象的字段。您不能(安全地)让多个线程调用s.setNum(n)
。但是,您可以让线程创建一个新的
Setter
对象,在其中存储一个数字,然后更改s
以引用新对象而不是旧对象。No, that use of
volatile
only applies to the references
, not the fields of the object it refers to. You can't (safely) have multiple threads calls.setNum(n)
.You could, however, have a thread create a new
Setter
object, store a number into it, and then changes
to refer to the new object instead of the old one.对对象的引用和该对象的内容是不同的东西。在您的情况下,对象的引用(是您的 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.