当前位置:网站首页>详解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方法:
边栏推荐
- 都能看懂的LIS(最长上升子序列)问题[通俗易懂]
- Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
- Case of camera opening by tour
- js数组拼接的四种方法[通俗易懂]
- Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
- Unity uses SQLite
- Business visualization - make your flowchart'run'up
- News classification based on LSTM model
- What is the difference between consonants and Initials? (difference between initials and consonants)
- Significance and measures of security encryption of industrial control equipment
猜你喜欢
随机推荐
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
NIO与传统IO的区别
js如何获取集合对象中某元素列表
Significance and measures of security encryption of industrial control equipment
能升职加薪?PMP证书含金量浅析
Using closures to switch toggle by clicking a button
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
MySQL清空表数据
基础—io密集型计算和cpu密集型计算
AirServer2022最新版功能介绍及下载
同花顺股票开户选哪个券商好手机开户是安全么?
locust 系列入门
JS how to get a list of elements in a collection object
使用闭包实现点击按钮切换 toggle
LIS (longest ascending subsequence) problem that can be understood [easy to understand]
Unity 使用Sqlite
收到一封CTO来信,邀约面试机器学习工程师
打出三位数的所有水仙花数「建议收藏」