当前位置:网站首页>详解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方法:
边栏推荐
- BlocProvider 为什么感觉和 Provider 很相似?
- 月入1W+的自媒体达人都会用到的运营工具
- LIS (longest ascending subsequence) problem that can be understood [easy to understand]
- 【STM32】STM32CubeMX教程二–基本使用(新建工程点亮LED灯)
- Flume面试题
- Electron学习(三)之简单交互操作
- Manually implement function isinstanceof (child, parent)
- 企业架构与项目管理的关联和区别
- Basic operation of binary tree
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
猜你喜欢

Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc

上半年暂停考试要补考?包含监理工程师、建筑师等十项考试

Basic operation of binary tree

焱融看 | 混合云时代下,如何制定多云策略

Aidl basic use

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue

I received a letter from CTO inviting me to interview machine learning engineer

Is PMP certificate really useful?

杰理之烧录上层版物料需要【篇】

杰理之蓝牙耳机品控和生产技巧【篇】
随机推荐
List announced | outstanding intellectual property service team in China in 2021
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Test cancellation 1
An operation tool used by we media professionals who earn 1w+ a month
Pytest Collection (2) - mode de fonctionnement pytest
PMP与NPDP之间的区别是什么?
Four methods of JS array splicing [easy to understand]
The correct way to set the bypass route
【单体】流辰信息I-BPSv3服务器推荐配置
ngnix基础知识
Spark面试题
What is the difference between PMP and NPDP?
Several ways of writing main function in C
MIT|256KB 内存下的设备上训练
Fundamentals - IO intensive computing and CPU intensive computing
Is PMP certificate really useful?
JS how to get a list of elements in a collection object
基于三维GIS的不动产管理应用
能升职加薪?PMP证书含金量浅析
Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan