long
和 double
读写操作不是原子的,因为它们的大小超过了 cpu 字的大小。
那么如果我有 64 位机器,我可以得到 long
和 double
的原子读写操作吗?
最佳答案
so could i get atomic read and write operation of long and double if i have 64 bit machine ?
答案是“也许”。答案取决于 JVM 实现以及机器架构。引用自Java Language definition 17.7 :
Some implementations may find it convenient to divide a single write action on a 64-bit long or double value into two write actions on adjacent 32-bit values. For efficiency's sake, this behavior is implementation-specific; an implementation of the Java Virtual Machine is free to perform writes to long and double values atomically or in two parts.
Implementations of the Java Virtual Machine are encouraged to avoid splitting 64-bit values where possible. Programmers are encouraged to declare shared 64-bit values as volatile or synchronize their programs correctly to avoid possible complications.
这是关于 Ensure atomicity when reading and writing 64-bit values 的精彩页面.
关于java - long 和 double 值的原子读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23120811/