在 Java 中编写 long 和 double 不是原子的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/517532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing long and double is not atomic in Java?
提问by fmsf
Reading and writing of a single variable is atomic (language guarantee!), unless the variable is of type long or double.
读取和写入单个变量是原子的(语言保证!),除非变量是 long 或 double 类型。
I was reading a course's slides and I found that written. The class was about concurrency.
我正在阅读一门课程的幻灯片,我发现它已经写好了。这门课是关于并发的。
Can anyone explain to me why writing a long or a double is not an atomic operation? It really took me by surprise.
谁能向我解释为什么写 long 或 double 不是原子操作?着实让我大吃一惊。
回答by Don Branson
It's not atomic because it's a multiple-step operation at the machine code level. That is, longs and doubles are longer than the processor's word length.
它不是原子的,因为它是机器代码级别的多步骤操作。也就是说,longs 和 doubles 比处理器的字长要长。
回答by daveb
Just to clarify the situation for Java, doubles and longs are not read or written to atomically unless they're declared volatile
只是为了澄清 Java 的情况,除非声明,否则不会以原子方式读取或写入双精度型和长型 volatile
回答by napster
Java long and double are not atomic in 32 bit machines, but atomic in 64 bit machines with some of the 64 bit JVMs. why its dependant on machine bit length? Because 32 bit machine needs two writes for long(as long is 64 bit). Read thisfor detailed info.
Java long 和 double 在 32 位机器中不是原子的,而是在带有一些 64 位 JVM 的 64 位机器中是原子的。为什么它依赖于机器位长度?因为 32 位机器需要两次写入 long(因为 long 是 64 位)。阅读本文了解详细信息。
回答by Jas Kaur
Many programmers must have read this statement in "3.1.2. Non-Atomic 64bit Operations" in java concurrency in practice . I have referred the latest JLS 17.7. Non-Atomic Treatment of double and long. They still nowhere claim that 64 bit jVM are norm these days. So 64 bit operations are broken into 32 bit operations that break atomicity and dangerous to use in multithreaded environment until declared volatile . Long and double are 64 bit long in java. So writing and reading operations are not atomic in java.
很多程序员在实践java并发中的《3.1.2.非原子64位操作》中的这段话想必都看过了。我已经提到了最新的 JLS 17.7。double 和 long 的非原子处理。如今,他们仍然没有声称 64 位 JVM 是常态。因此,64 位操作被分解为 32 位操作,这些操作破坏了原子性,并且在声明 volatile 之前在多线程环境中使用是危险的。Long 和 double 在 java 中是 64 位长。所以写和读操作在java中不是原子的。
回答by digvijayb
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.
Java 编程语言内存模型,对非易失性 long 或 double 值的单次写入被视为两次单独的写入:一次写入每个 32 位一半。这可能导致线程从一次写入中看到 64 位值的前 32 位,而从另一次写入中看到后 32 位的情况。
回答by Kanagavelu Sugumar
Read answer by maaartinus @ What operations in Java are considered atomic?
Read answer by Jon Skeet @ When primitive datatypes not thread-safe in Java?
阅读 maaartinus 的回答@Java 中的哪些操作被认为是原子的?
阅读 Jon Skeet 的回答@当原始数据类型在 Java 中不是线程安全的?
As per the JLSyou can make read and write operation on double and long to be atomic by declaring it as volatile. But this will not ensure ++ to be atomic. That needs concurrent.atomic package.
Read this answerfrom Louis Wasserman.
根据JLS,您可以通过将 double 和 long 声明为 volatile 来使读和写操作成为原子操作。但这并不能确保 ++ 是原子的。那需要 concurrent.atomic 包。
阅读Louis Wasserman 的回答。
Also this blogand comments.
还有这个博客和评论。

