在java中,当Long, long, Double,double等变量自动装箱或拆箱时,读写操作是原子的吗?

In java, when variables such as Long, long, Double ,double are autoboxing or unboxing, is reading or writing operation atomic?

我知道this

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double). Reads and writes are atomic for all variables declared volatile (including long and double variables).

但是我想知道 Long, long, Double ,double 等变量自动装箱或拆箱时,读写操作是原子的吗?

例如:

private Long a;    
private long b;    
private Double c;    
private double d;

a = 2; //is this operation atomic?    
b = a; //is this operation atomic?    
d = 3;    
c = d; //is this operation atomic

你说:

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double)

a = 2;

这是对引用变量的写入,所以它是原子的

b = a;

这相当于

read a
call a.longValue()
assign result to b

所以这会读取一个引用变量(原子的),然后从 Long 对象中获取一个不可变的 long 值(因此原子性无关紧要)并写入一个 long 基元(因此不能保证是原子的)

d = 3;

这写入原始双精度数(因此不能保证是原子的)

c = d;

这相当于

read d
call Double.valueOf(value)
assign result to b

所以这从原始双精度数(因此不能保证是原子的)读取,然后将值转换为双精度数并将该引用写入引用变量(原子的)