当前位置:网站首页>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
边栏推荐
- LeetCode 83. Delete duplicate elements in the sorting linked list
- [C language] simple implementation of mine sweeping game
- The difference between session and cookies
- ROS create workspace
- LeetCode 283. Move zero
- 锐捷EBGP 配置案例
- Redis Key-Value数据库 【高级】
- Lambda expressions and method references
- Problems encountered in uni app development (continuous update)
- Little bear sect manual query and ADC in-depth study
猜你喜欢

Data playback partner rviz+plotjuggler

ROS2----LifecycleNode生命周期节点总结

Let every developer use machine learning technology

Pbootcms collection and warehousing tutorial quick collection release

VLAN experiment of switching technology

Memcached installation

sudo提权

LeetCode 90. 子集 II

深入学习JVM底层(二):HotSpot虚拟机对象

Ros2 --- lifecycle node summary
随机推荐
Ti millimeter wave radar learning (I)
Talking about MySQL database
State machine in BGP
Linear DP (split)
VLAN experiment of switching technology
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Go learning notes integration
Frequently asked questions about jetpack compose and material you
Spark overview
AttributeError: ‘str‘ object has no attribute ‘decode‘
Current situation analysis of Devops and noops
格式校验js
复杂 json数据 js前台解析 详细步骤《案例:一》
[C language] simple implementation of mine sweeping game
Support new and old imperial CMS collection and warehousing tutorials
Detailed steps of JS foreground parsing of complex JSON data "case: I"
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Eco express micro engine system has supported one click deployment to cloud hosting
LeetCode 77. combination
New version of dedecms collection and release plug-in tutorial tool