当前位置:网站首页>并发三大特性1-可见性
并发三大特性1-可见性
2022-06-22 08:09:00 【Hide on jdk】
可见性:当一个线程修改了共享变量的值,其他线程能够看到修改的值
底层是如何保证可见性?
1.通过 volatile 关键字保证可见性。
2.通过 内存屏障保证可见性。
3.通过 synchronized 关键字保证可见性。
4.通过 Lock保证可见性。
5.通过 final 关键字保证可见性
主内存与工作内存之间如何进行数据交互?
通过8大原子操作:
lock(锁定):作用于主内存的变量,把一个变量标识为一条线程独占状态。
unlock(解锁):作用于主内存变量,把一个处于锁定状态的变量释放出来,释放
后的变量才可以被其他线程锁定。
read(读取):作用于主内存变量,把一个变量值从主内存传输到线程的工作内存
中,以便随后的load动作使用
load(载入):作用于工作内存的变量,它把read操作从主内存中得到的变量值放
入工作内存的变量副本中
use(使用):作用于工作内存的变量,把工作内存中的一个变量值传递给执行引
擎,每当虚拟机遇到一个需要使用变量的值的字节码指令时将会执行这个操作。
assign(赋值):作用于工作内存的变量,它把一个从执行引擎接收到的值赋值给
工作内存的变量,每当虚拟机遇到一个给变量赋值的字节码指令时执行这个操作。
store(存储):作用于工作内存的变量,把工作内存中的一个变量的值传送到主
内存中,以便随后的write的操作。
write(写入):作用于主内存的变量,它把store操作从工作内存中一个变量的值
传送到主内存的变量中。
volatile的特性
可见性:对一个volatile变量的读,总是能看到(任意线程)对这个volatile变量最
后的写入。
原子性:对任意单个volatile变量的读/写具有原子性,但类似于volatile++这种复
合操作不具有原子性
有序性:对volatile修饰的变量的读写操作前后加上各种特定的内存屏障来禁止指
令重排序来保障有序性
volatile在hotspot的实现
字节码解释器实现
结论:x86处理器中利用lock实现类似内存屏障的效果
汇编层面volatile的实现
添加下面的jvm参数查看之前可见性Demo的汇编指令
| ‐XX | : | +UnlockDiagnosticVMOptions | ‐XX | : | +PrintAssembly | ‐Xcomp |
边栏推荐
- Asynchronous sending and receiving of message passing based concurrent programming (MPI)
- The difference between get and post requests
- DTD constraints
- Modular import and export collation in JS
- [Oracle database] mammy tutorial Day12 character function
- MySQL index
- 关于菲涅尔现象
- QT custom composite control (class promotion function)
- MySQL avoids the method of repeatedly inserting records (ignore, replace, on duplicate key update)
- 【宋红康 MySQL数据库 】【高级篇】【07】MySQL的存储引擎
猜你喜欢

【 Oracle database】 Nursery Mother Tutorial day13 date Function
![[Oracle database] mammy tutorial day13 date function](/img/ca/90aaa682ec393b1531060377276ca6.png)
[Oracle database] mammy tutorial day13 date function

Kubernetes practice

QT 自定义组合控件(类提升功能)

Usage and understanding of async/await in JS

0基础自学stm32(野火)——什么是寄存器?

It operation and maintenance knowledge map

Using KDJ metrics on MT4

(7) Bidirectional linked list

MySQL transactions
随机推荐
Thoughts on the construction of data analysis platform for small and medium-sized enterprises (I)
Docker install redis
矩阵运算
0 basic self-study STM32 (wildfire) -- what is a register?
What are uniapp, wap2app and 5 + app in dccloud?
【Oracle 数据库】奶妈式教程 day11 数值函数
Submit values of various inputs of the form
先锋期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
MySQL query database capacity
模板代码概述
【Oracle 数据库】奶妈式教程 day14 转换函数
Detailed explanation of subnet mask
[Oracle database] mammy tutorial day14 conversion function
DTD constraints
How to design the dead shot, the best and eye-catching performance of the watch Vanguard
Model electricity experiment -- Experiment 1 transistor common emitter single transistor amplifier
【Oracle 数据库】奶妈式教程 day13 日期函数
Mt4/mql4 getting started to be proficient in EA tutorial lesson 7 - common functions of MQL language (VII) - index value taking function
Idea reports an error "insufficient memory"
(7) Bidirectional linked list