当前位置:网站首页>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.54-染色法判定二分图
- 深入学习JVM底层(二):HotSpot虚拟机对象
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- VLAN experiment of switching technology
- Singleton mode compilation
- Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
- Redis key value database [primary]
- Format check JS
- The official zero foundation introduction jetpack compose Chinese course is coming!
- Frequently asked questions about jetpack compose and material you
猜你喜欢
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
来自读者们的 I/O 观后感|有奖征集获奖名单
让每一位开发者皆可使用机器学习技术
Data playback partner rviz+plotjuggler
Sumo tutorial Hello World
Let every developer use machine learning technology
Current situation analysis of Devops and noops
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
随机推荐
I/o multiplexing & event driven yyds dry inventory
Zhuanzhuanben - LAN construction - Notes
In depth understanding of JUC concurrency (I) what is JUC
Introduce uview into uni app
Redis key value database [seckill]
I/o impressions from readers | prize collection winners list
Picture clipping plug-in cropper js
Scheme and implementation of automatic renewal of token expiration
ES6的详细注解
LeetCode 77. combination
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
神机百炼3.53-Kruskal
Servlet web XML configuration details (3.0)
Let every developer use machine learning technology
Eco express micro engine system has supported one click deployment to cloud hosting
LeetCode 47. Full arrangement II
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Community theory | kotlin flow's principle and design philosophy
The difference between session and cookies
使用HBuilderX的一些常用功能