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

查看:144
本文介绍了Java volatile引用与AtomicReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

volatile 对象引用和 AtomicReference 之间是否有任何区别,以防我使用 get() and set() -methods from AtomicReference

Is there any difference between a volatile Object reference and AtomicReference in case I would just use get() and set()-methods from AtomicReference?

推荐答案

简短的答案是:不。

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.htmlrel =nofollow noreferrer> java.util.concurrent.atomic package doc a>:

From the java.util.concurrent.atomic package doc:


原子的访问和更新的内存效果通常遵循挥发性物质的规则:

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, the doc for the package is very good and everything is explained...

lazySet (在Java 6中引入)操作引入了语义不可能通过 volatile 变量;请参阅此信息了解详情。

lazySet (introduced in Java 6) is a newer operation introduced that has semantics unachievable through volatile variables; see this post for more information.

这篇关于Java volatile引用与AtomicReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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