Java 中的 64 位赋值在 32 位机器上是原子的吗? [英] Are 64 bit assignments in Java atomic on a 32 bit machine?
问题描述
如果我有这样的代码 -
If I have code like this -
long x;
x = 0xFFFFFFFFL;
如果我在 32 位机器上运行此代码,它是否保证是原子的,或者是否有可能读取 x 的不同线程可能会得到不完整/垃圾值?
If i run this code on a 32 bit machine is it guaranteed to be atomic or is it possible that a different thread reading x, might get an incomplete/garbage value?
推荐答案
以下是简短摘要:
- 对于参考,读/写始终是原子的(即使在 64 位实现中!)
- 对于
int
、char
、byte
、short
、boolean
、float
,读/写总是原子 - 对于
double
和long
,如果它们是volatile
,读/写总是原子莉>
- For references, reads/writes are ALWAYS atomic (even in 64 bit implementations!)
- For
int
,char
,byte
,short
,boolean
,float
, reads/writes are ALWAYS atomic - For
double
andlong
, if they'revolatile
, reads/writes are ALWAYS atomic
因此只有读/写可能不是原子的例外:
Therefore there is only exception where reads/writes may not be atomic:
- 对于
double
和long
,如果它们NOT被声明为volatile
,它们是不保证是原子的
- For
double
andlong
, if they're NOT declaredvolatile
, they're NOT GUARANTEED to be atomic
因此就读/写共享数据的原子性而言,您只需要将volatile
设为任意double
或long
即可.不管实际实现中使用了多少位,其他一切都已经保证是原子的.
So as far as atomicity of reading/writing shared data is concerned, you only need to make volatile
any double
or long
. Everything else is already guaranteed to be atomic, regardless of how many bits are used in actual implementation.
此处复制了相关部分以供快速参考:
Here's the relevant section reproduced here for quick reference:
某些实现可能会发现将 64 位 long
或 double
值上的单个写入操作划分为相邻 32 位值上的两个写入操作很方便.为了效率起见,这种行为是特定于实现的;Java 虚拟机可以自由地以原子方式或分两部分写入 long
和 double
值.
JLS 17.7 Non-atomic Treatment of
double
andlong
Some implementations may find it convenient to divide a single write action on a 64-bit
long
ordouble
value into two write actions on adjacent 32 bit values. For efficiency's sake, this behavior is implementation specific; Java virtual machines are free to perform writes tolong
anddouble
values atomically or in two parts.
就 Java 编程语言内存模型而言,对非 volatile long
或 double
值的单次写入被视为两次单独的写入:一次写入32 位一半.这可能导致线程从一次写入中看到 64 位值的前 32 位,而从另一次写入中看到后 32 位的情况.volatile long
和 double
值的写入和读取始终是原子的.对引用的写入和读取始终是原子的,无论它们是实现为 32 位值还是 64 位值.
For the purposes of the Java programming language memory model, a single write to a non-volatile long
or double
value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64 bit value from one write, and the second 32 bits from another write. Writes and reads of volatile long
and double
values are always atomic. Writes to and reads of references are always atomic, regardless of whether they are implemented as 32 or 64 bit values.
鼓励 VM 实现者尽可能避免拆分其 64 位值.鼓励程序员将共享的 64 位值声明为 volatile
或正确同步他们的程序以避免可能出现的复杂情况.
VM implementors are encouraged to avoid splitting their 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.
另见
- JLS 17.6 单词撕裂 - 保证例如一个
byte
可以在没有邻居干扰的情况下更新 - JLS 8.3.1.4
volatile
字段 - Java 教程/基本类/并发/原子变量
- 注意,给定
int i;
,i++
是 NOT 原子的! - JLS 17.6 Word Tearing - guarantees e.g. a
byte
can be updated without neighbor interference - JLS 8.3.1.4
volatile
Fields - Java Tutorials/Essential Classes/Concurrency/Atomic Variables
- Note that given
int i;
,i++
is NOT atomic! - 使用 volatile long 有什么意义吗?
- 如何在 Java 中声明数组元素 volatile?
- A
volatile long[]
是volatile
reference 对long
数组的引用 long
元素本身不是volatile
- Is there any point in using a volatile long?
- How to declare array elements volatile in Java?
- A
volatile long[]
isvolatile
reference to an array oflong
- The
long
elements themselves are notvolatile
这篇关于Java 中的 64 位赋值在 32 位机器上是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- A
- A
- Note that given
See also
- 注意,给定