java - long 和 double 值的原子读写

标签 java multithreading atomic

longdouble 读写操作不是原子的,因为它们的大小超过了 cpu 字的大小。

那么如果我有 64 位机器,我可以得到 longdouble 的原子读写操作吗?

最佳答案

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/

相关文章:

java - 在 SimpleDateFormat() 中更改时区

Java 同步块(synchronized block)未按预期工作

c++ - 这是原子读写 bool 的正确方法吗?

c++ - 我可以在单读/单写队列中用 volatile 替换原子吗?

c# - 剪刀布石头算法

java - jpql错误在哪里附近

java - 从线程返回图像

atomic - _mm_mwait 如何工作?

java - 将 SWT 应用程序编译为 native 的优点和缺点

c# - 我如何在 C# 中使用线程一次从网络加载多个图像?