当前位置:网站首页>【JUC】并发需要了解的关键字volatile
【JUC】并发需要了解的关键字volatile
2022-07-25 23:23:00 【夜七天】
并发需要了解的关键字volatile
1. 关键字volatile是什么?
volatile是Java虚拟机提供的轻量级同步机制。
它的三个特性分别是:
1> 可见性
2> 不保证原子性
3> 禁止指令重排
具体特性的详细解释这里不赘述;
1.1可见性
volatile修饰的变量可见性主要体现在如下几方面:
- 任意线程对volatile修饰的变量的写会立刻同步到主内存中;
- 其他线程读取该变量会读取到最新的值,也就是说最新一次写的内容;

1.2不保证原子性
并发情况下会出现这样的情况:
1.3禁止指令重排
计算机在执行程序的时候, 为了提高性能, 编译器和处理器会对指令进行重排序.
1.单线程环境, 重排序能保证程序的最终结果和顺序执行的结果一致
2.处理器处理重排序必须考虑指令之间的数据依赖性(如b依赖a, 不能将b排序在a之前执行)
3.多线程调度过程中, 由于重排序存在, 两个线程的变量无法保证一致性

3. 关键字volatile适用于什么场景?
volatile可以理解为轻量级的synchronized, 最适用一个线程写,多个线程读的场合。
我们在java.util.concurrent.atomic的原子类中发现,使用volatile关键字实现原子性;
4. 实际使用建议
如果自己使用volatile修饰变量,一定要注意volatile的特性在业务场景中是否适用。
边栏推荐
- [QNX hypervisor 2.2 user manual]9.7 generate
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- PHP JSON variable array problem
- Why are there many snapshot tables in the BI system?
- POI特效 市场调研
- Multimodal deep multi modal sets
- 日期类的实现
- Node基础
- JS regular expression content:
- Recommended system - an embedded learning framework for numerical features in CTR prediction
猜你喜欢

WebMvcConfigurationSupport

wordpress去掉网站发布时间

Firewall command simple operation

Apple CMS V10 template /mxone Pro adaptive film and television website template

idea设置get、set模板解决boolean类型字段的命名问题

Node基础

XxE & XML external entity injection utilization and bypass

类和对象(3)

How to set pseudo static for WordPress fixed links

学习探索-3d轮播卡片
随机推荐
TS basic data type
ETL工具(数据同步) 二
R language drawing parameters (R language plot drawing)
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
学习探索-3d轮播卡片
Yii2 behavior usage and its calling method
[code case] blog page design (with complete source code)
Source code of YY music wechat applet imitating Netease cloud music
1913. Maximum product difference between two number pairs - no sorting required
Practical skills of easyexcel
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
762. Prime number calculation setting in binary representation
WebMvcConfigurationSupport
Expression of directional signal -- complex exponential signal
学习探索-波浪
Idea sets get and set templates to solve the naming problem of boolean type fields
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
新手开户选择哪个券商公司好呢?安全吗
Ffmpeg first learning (only for coding)
Strategy mode_