当前位置:网站首页>【多线程】long和double的非原子性协定
【多线程】long和double的非原子性协定
2022-07-28 08:32:00 【檀越剑指大厂】
long和double的非原子性协定
Java内存模型要求lock、unlock、read、load、assign、use、store、write这8个操作都具有原子性,但是对于64位的数据类型(double、long)定义了相对宽松的规定:允许虚拟机将没有被volatile修饰的64位数据的读写操作划分为两次的32位操作来进行,即允许虚拟机可以不保证64位数据类型的load、store、read和write操作的原子性。
非原子性协定可能导致的问题
如果有多个线程共享一个未申明为volatile的long或double类型的变量,并且同时对其进行读取和修改操作,就有可能会有线程读取到"半个变量"的数值或者是一半正确一半错误的失效数据。
在实际应用中的解决
因为上述可能造成的问题,势必在对long和double类型变量操作时要加上volatile关键字,实际上如下:
1、64位的java虚拟机不存在这个问题,可以操作64位的数据
2、目前商用JVM基本上都会将64位数据的操作作为原子操作实现
所以我们编写代码时一般不需要将long和double变量专门申明为volatile
边栏推荐
- 蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘
- Code management platform SVN deployment practice
- KEGG通路的从属/注释信息如何获取
- When I use MySQL CDC, there are 100 million pieces of data in the source table. In the full volume phase, when I synchronize 10 million, I stop, and then pass
- ES6 let and Const
- Sentinel
- 网络层的IP协议
- VS2015使用dumpbin 查看库的导出函数符号
- Data analysis interview question summary
- 推荐一个摆脱变量名纠结的神器和批量修改文件名方法
猜你喜欢
![Vrrp+mstp configuration details [Huawei ENSP experiment]](/img/44/987e9ff87f5c8e2a2ab493e947f884.png)
Vrrp+mstp configuration details [Huawei ENSP experiment]

【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared

Overview of head pose estimation

Machine learning: self paced and fine tuning

快速上手Flask(一) 认识框架Flask、项目结构、开发环境

Go waitgroup and defer

2022年安全员-B证考试模拟100题及答案

C language array pointer and pointer array discrimination, analysis of memory leakage

Design for failure常见的12种设计思想

What are the main uses of digital factory management system
随机推荐
Train your own classification [Bao Jiaobao, the data are ready]
2022安全员-C证特种作业证考试题库及答案
【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
Why setting application.targetframerate doesn't work
3D全景展示新模式,成为破局的关键
Kubernetes cluster configuration DNS Service
shell 实现harbor v1/v2的备份/恢复/迁移等功能
Prometheus TSDB analysis
12 common design ideas of design for failure
mysql5.7.38容器里启动keepalived
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
Sword finger offer
Implementation principle of golang synergy
Marketing play is changeable, and understanding the rules is the key!
7 C控制语句:分支和跳转
Machine learning: self paced and fine tuning
Introduction to official account
2022年安全员-B证考试模拟100题及答案
Path and attribute labels of picture labels
C simply call FMU for simulation calculation