当前位置:网站首页>详解Volatile关键字
详解Volatile关键字
2022-07-01 21:43:00 【键盘歌唱家】
一、简介
1.Volatile内存语义
1.当写一个volatile变量时,JMM会把该线程对应的本地内存中的共享变量值立即刷新回主内存中。
2.当读一个volatile变量时,JMM会把该线程对应的本地内存设置为无效,重新回到主内存中读取最新共享变量。
3.所以volatile的写内存语义是直接刷新到主内存中,读的内存语义是直接从主内存中读取。
2.两大特性
1.可见性 :写完后立即刷新回主内存并及时发出通知,大家可以去主内存拿最新版,前面的修改对后面所有线程可见!
2.禁止指令重排:重排序是指编译器和处理器为了优化程序性能而对指令序列进行重新排序的一种手段,有时候会改变程序语句的先后顺序。不存在数据依赖关系,可以重排序;存在数据依赖关系,禁止重排序。但重排后的指令绝对不能改变原有的串行语义!这点在并发设计中必须要重点考虑!
3.上面的禁止指令重排是基于内存屏障实现的:
粗分两种
1.读屏障:在读指令之前插入读屏障,让工作内存或CPU高速缓存当中的缓存数据失效,重新回到玉内存中获取最新数据。
2.写屏障:在写指令之后插入写屏障,强制把写缓冲区的数据刷回到主内存中。
在Java层面的Unsafe类就有以下的native方法:
边栏推荐
- Several ways of writing main function in C
- 基于三维GIS的不动产管理应用
- 最近公共祖先(LCA)在线做法
- 杰理之关于长按开机检测抬起问题【篇】
- Why does blocprovider feel similar to provider?
- 指标陷阱:IT领导者易犯的七个KPI错误
- 收到一封CTO来信,邀约面试机器学习工程师
- 基于LSTM模型实现新闻分类
- Difference and use between require and import
- Fundamentals - IO intensive computing and CPU intensive computing
猜你喜欢
![[deep learning] use deep learning to monitor your girlfriend's wechat chat?](/img/03/ecf50eacc91c0633b0d9689cdad2c2.png)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?

Spark面试题

Talking from mlperf: how to lead the next wave of AI accelerator
![[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分

100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖

Basic operation of binary tree

【深度学习】利用深度学习监控女朋友的微信聊天?

MIT|256KB 内存下的设备上训练

基于LSTM模型实现新闻分类

Flume面试题
随机推荐
ngnix基础知识
Burpsuite simple packet capturing tutorial [easy to understand]
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
手动实现function isInstanceOf(child,Parent)
MySQL series transaction log redo log learning notes
Count the number of each character in the character
Separate the letters and numbers in the string so that the letters come first and the array comes last
指标陷阱:IT领导者易犯的七个KPI错误
C中main函数的几种写法
Training on the device with MIT | 256Kb memory
A debugging to understand the slot mechanism of redis cluster
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
统计字符中每个字符出现的个数
基于三维GIS的不动产管理应用
旁路由设置的正确方式
为什么数字化转型战略必须包括持续测试?
NIO与传统IO的区别
函数基本学习之一
String类型转换BigDecimal、Date类型
PCB plug hole technology~