当前位置:网站首页>【多线程】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
边栏推荐
- Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
- 51单片机存储篇:EEPROM(I2C)
- VR全景拍摄,助力民宿多元化宣传
- Go waitgroup and defer
- 训练一个自己的分类 | 【包教包会,数据都准备好了】
- Vs2015 use dumpbin to view the exported function symbols of the library
- 2022年危险化学品经营单位安全管理人员上岗证题目及答案
- Go interface advanced
- LeetCode_406_根据身高重建队列
- Code management platform SVN deployment practice
猜你喜欢

Overview of head pose estimation

No one wants to tell the truth about kubernetes secret

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

Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)

公众号简介

C#简单调用FMU ,进行仿真计算

从开发转测试:我从零开始,一干就是6年的自动化测试历程

阿里云服务器搭建和宝塔面板连接

IDC脚本文件运行

Implementation principle of golang synergy
随机推荐
2022高压电工考试模拟100题及模拟考试
一款入门神器TensorFlowPlayground
Different HR labels
JSON 文件存储
Detailed explanation of switch link aggregation [Huawei ENSP]
Prometheus TSDB analysis
Centralized log management with sentry
How to execute the SQL assembled in ODPs SQL function and get the return value?
How to obtain the subordinate / annotation information of KEGG channel
2022年危险化学品经营单位安全管理人员上岗证题目及答案
A perfect cross compilation environment records the shell scripts generated by PETA
Sentry log management system installation and use tutorial
Marketing play is changeable, and understanding the rules is the key!
[592. Fraction addition and subtraction]
NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)
mysql5.7.38容器里启动keepalived
CSV文件存储
中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
象棋机器人夹伤7岁男孩手指,软件测试工程师的锅?我笑了。。。