atomicity and volatile
来源:互联网 发布:终极一班武器淘宝 编辑:程序博客网 时间:2024/05/23 18:41
http://stackoverflow.com/questions/362740/java-memory-model-can-someone-explain-it
I will offer some insight on just one aspect, because it's often misunderstood:
There's a difference between volatility and atomicity. People often think that an atomic write is volatile (i.e. you don't need to worry about the memory model if the write is atomic). That's not true.
Volatility is about whether one thread performing a read (logically, in the source code) will "see" changes made by another thread.
Atomicity is about whether there is any chance that if a change is seen, only part of the change will be seen.
For instance, take writing to an integer field. That is guaranteed to be atomic, but not volatile. That means that if we have (starting at foo.x = 0):
Thread 1: foo.x = 257;Thread 2: int y = foo.x;
It's possible for y
to be 0 or 257. It won't be any other value, (e.g. 256 or 1) due to the atomicity constraint. However, even if you know that in "wall time" the code in thread 2 executed after the code in thread 1, there could be odd caching, memory accesses "moving" etc. Making the variable x
volatile will fix this.
- atomicity and volatile
- Should volatile Acquire Atomicity and Thread Visibility Semantics?
- JAVA 原子性和波动性总结 Atomicity and Volality
- atomicity and volatility 代替synchronized 对象锁 的另一种方法(原子操作+可见性)
- Const and volatile
- static and volatile
- mutable and volatile
- volatile and const 用法
- Const and volatile
- Java volatile and transient
- static and volatile
- volatile and synchronized
- volatile and transient
- volatile and non-cacheable
- volatile and synchronized
- volatile and mutable and extern
- 原子性(atomicity)
- Difference Between Static and Volatile
- 给myeclipse安装svn插件的各种方法
- web开发(一)
- C++中使子函数返回多个值
- 编译libtiff和libgeotiff
- TCP/IP,http,socket,长连接,短连接——小结
- atomicity and volatile
- hive交互式模式 命令大全
- .Net MVC4 使用心得(四)分页、Partial View、删除、ajax返回
- linux 环境变量设置
- 数据挖掘技术--多维数据模型
- 一张图表 看懂保险
- hive经典的入门流程操作
- 一张图表 看懂保险
- 读书笔记: 线程安全性