Java volatile 引用与 AtomicReference [英] Java volatile reference vs. AtomicReference
问题描述
如果我只使用 get()
和 set()
-来自 AtomicReference
?
Is there any difference between a volatile
Object reference and AtomicReference
in case I would just use get()
and set()
-methods from AtomicReference
?
推荐答案
简短的回答是:没有.
来自 java.util.concurrent.atomic
包文档.引用:
From the java.util.concurrent.atomic
package documentation. To quote:
原子的访问和更新的记忆效应通常遵循易失性规则:
The memory effects for accesses and updates of atomics generally follow the rules for volatiles:
get
具有读取volatile
变量的记忆效应.set
具有写入(赋值)volatile
变量的记忆效应.
get
has the memory effects of reading avolatile
variable.set
has the memory effects of writing (assigning) avolatile
variable.
顺便说一下,该文档非常好,并且解释了所有内容.
By the way, that documentation is very good and everything is explained.
AtomicReference::lazySet
是一种较新的 (Java 6+) 操作,其语义无法通过 volatile
变量实现.请参阅这篇文章了解更多信息.
AtomicReference::lazySet
is a newer (Java 6+) operation introduced that has semantics unachievable through volatile
variables. See this post for more information.
这篇关于Java volatile 引用与 AtomicReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!