原子读写long和double值 [英] Atomic read and write of long and double values
问题描述
long
和 double
读取和写入操作不是原子的,因为它们的大小超过了cpu字大小。
long
and double
read and write operation are not atomic because of their size more than cpu word size.
我可以获得 long
和 double的原子读写操作
如果我有64位机器?
So could I get atomic read and write operation of long
and double
if I have 64 bit machine?
推荐答案
所以我可以获得原子读写如果我有64位机器,则操作long和double?
so could i get atomic read and write operation of long and double if i have 64 bit machine ?
答案是可能。答案取决于JVM实现以及机器架构。引用 Java语言定义17.7 :
The answer is "maybe". The answer depends on the JVM implementation as well as the machine architecture. To quote from the Java Language definition 17.7:
某些实现可能会发现将64位长或双值上的单个写操作分成相邻32位的两个写操作很方便位值。为了效率,这种行为是特定于实现的; Java虚拟机的实现可以原子地或分两部分对long和double值执行写入。
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.
鼓励实现Java虚拟机以避免拆分64尽可能使用比特值。我们鼓励程序员将共享的64位值声明为volatile或正确同步其程序以避免可能的复杂情况。
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.
这是一个很棒的页面确保原子性时读取和写入64位值。
这篇关于原子读写long和double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!