Java volatile 引用与 AtomicReference [英] Java volatile reference vs. AtomicReference

查看:11
本文介绍了Java volatile 引用与 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 a volatile variable.
  • set has the memory effects of writing (assigning) a volatile 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆