当前位置:网站首页>In depth understanding of JUC concurrency (II) concurrency theory
In depth understanding of JUC concurrency (II) concurrency theory
2022-07-02 06:14:00 【I think starfish_ ninety-eight】
List of articles
Concurrency theory
Java Memory model (JMM)
First , What mechanism is used to exchange information between threads , There are two main types : Shared memory and messaging .Java The memory model is Shared memory Concurrent model of , Threads mainly pass through read - Write shared variables To complete implicit communication
Concept
The above said Shared memory Model refers to Java Memory model ( abbreviation JMM):
- Java The memory model determines when a thread's write to a shared variable is visible to another thread
- Shared variables between threads are stored in Main memory (main memory) in , Each thread has one Private local memory (local memory), The thread is stored in local memory to read / Write a copy of the shared variable
The picture shows , Threads A With threads B If you want to communicate with each other , Must go through the following 2 A step :
- First , Threads A Put the local memory A The shared variables updated in are refreshed to main memory
- then , Threads B Read threads into main memory A Previously updated shared variables
JVM Yes Java Implementation of memory model
Java The memory model divides memory into two parts : Thread stack area and heap area , The following figure shows Java The memory model is JVM Logical view in :
Stack area and heap area
JVM Each thread running in has its own thread stack , The thread stack contains information about the method calls executed by the current thread , We also call it call stack . As the code continues to execute , The call stack changes constantly
Thread stack :
- All local variable information of the current method
- All primitive types (boolean,byte,short,char,int,long,float,double) The local variables of are directly saved in the online process stack
Heap area : Contains Java Application created All object information , No matter which thread created the object
Reordering and data dependency
Reorder
When executing the program , To improve performance , Compilers and processors reorder instructions
- Compiler optimization reorder : Compiler without changing the semantics of single thread program , Sure Rearrange the execution order of statements .
- Instruction level parallel reordering : If there is no data dependency , The processor can Change the execution order of the machine instructions corresponding to the statements .
- Reordering of memory system : The processor uses cache and read / write buffer , This makes loading and storage operations appear to be out of order .
Memory barrier (Memory Barrier)
The compiler and CPU Ability to reorder instructions , Guarantee the same result in the end , Try to optimize performance . But insert one Memory barrier Tell the compiler and CPU: No matter what the order is, it can't be with this Memory barrier Reorder
use volatile Realization : Memory barrier Another thing to do is to force all kinds of CPU cache , Like a Write barrier All data previously written to the cache will be flushed , therefore , whatever CPU All threads on can read the latest version of these data ( That is to say volatile, Keep up to date )
Data dependency
- If two operations access the same variable , One of them is write operation , At this time, there is Data dependency
- Compilers and processors do not change the existence Data dependency The execution order of the two operations of the relationship , It doesn't reorder
as-if-serial and happens-before The rules
as-if-serial
No matter how reorder , The execution result under single thread cannot be changed , compiler 、runtime And the processor must comply as-if-serial semantics
happens-before
If the execution result of one operation needs to be visible to another operation , Then there must be... Between these two operations happens-before Relationship , The two operations of this can be performed on the same thread , It can also be in two different threads
happens-before The rules
- Rules of procedure sequence : Every operation in a thread ,happens-before Any subsequent operation in this thread
- Monitor lock rules : Unlocking a lock ,happens-before In the subsequent locking operation of this lock
- volatile Domain rules : To a volatile Write operation of domain ,happens-before Apply to any subsequent thread on this volatile Domain reading
- Transitive rules : If A happens-before B, And B happens-before C, that A happens-before C
边栏推荐
- 神机百炼3.53-Kruskal
- LeetCode 83. 删除排序链表中的重复元素
- Page printing plug-in print js
- Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
- 线性dp(拆分篇)
- It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
- VLAN experiment of switching technology
- Frequently asked questions about jetpack compose and material you
- ZABBIX server trap command injection vulnerability (cve-2017-2824)
- Introduce uview into uni app
猜你喜欢
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Monitoring uplink of VRRP
In depth understanding of JUC concurrency (I) what is JUC
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
Google Play Academy 组队 PK 赛,正式开赛!
注解和反射详解以及运用
让每一位开发者皆可使用机器学习技术
Frequently asked questions about jetpack compose and material you
随机推荐
New version of dedecms collection and release plug-in tutorial tool
Frequently asked questions about jetpack compose and material you
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Redis key value database [primary]
Google play academy team PK competition, official start!
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
【C语言】简单实现扫雷游戏
浏览器原理思维导图
Page printing plug-in print js
深入学习JVM底层(五):类加载机制
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
Mock simulate the background return data with mockjs
神机百炼3.52-Prim
Pbootcms collection and warehousing tutorial quick collection release
AttributeError: ‘str‘ object has no attribute ‘decode‘
How to use mitmproxy
[C language] screening method for prime numbers
Reading classic literature -- Suma++
深入学习JVM底层(四):类文件结构
I/o multiplexing & event driven yyds dry inventory