当前位置:网站首页>Three properties of concurrency
Three properties of concurrency
2022-07-26 11:17:00 【qq_ five hundred and forty-seven million twenty-six thousand on】
1. Atomicity
One or more operations Either all of them are executed and the execution process will not be interrupted by any factors , Or they don't do it .
- for example :a++; This operation is actually divided into the following 3 steps :
(1) Read a Value ;
(2) take a The value of the add 1;
(3) Will add 1 Assigned to a;
When multiple threads operate this statement at the same time , If A Thread just right a Variables to +1 You haven't had time to reassign to a, I was B Thread read a Then the value of +1 The operation of , that A The thread did not complete this 3 Step operation is B The thread reads this variable , There's no guarantee of atomicity . - How to guarantee atomicity
(1) adopt synchronized Keyword defines synchronization code blocks or synchronization methods to ensure atomicity .
(2) adopt Lock Interface guarantees atomicity .
(3) adopt Atomic Type guarantees atomicity .
2. visibility
Modification of shared variables by a thread , Can be seen by other threads in time .
- Shared variables
If a variable has copies in the working memory of multiple threads , So this variable is the shared variable of these threads . - Java Memory model
Java Memory model (Java Memory Model) It describes Java Various variables in the program ( Shared variables ) Access rules for , And in JVM Low level information such as storing variables in memory and reading variables from memory .
(1) All variables are stored in main memory ;
(2) Each thread has its own working memory , It stores a copy of the variables used by the thread .
(3) Each thread must operate on shared variables in its own working memory , Can't read and write directly from main memory .
(4) Different threads cannot directly access variables in each other's working memory , The transfer of variable values between different threads needs to be completed through the main memory .
- How visibility works
(1) First update the variable value modified in the thread to the independent working memory of the thread , Then refresh the value in the working memory to the main memory .
(2) The main memory will refresh the latest shared variable value to other working memory . - How to ensure visibility
(1) adopt synchronized Keyword defines synchronization code blocks or synchronization methods to ensure visibility .
(2) adopt volatile Keyword marked memory barrier ensures visibility .volatile The special rule of ensures that the new value can be synchronized to the main memory immediately , And refresh from main memory immediately before each use . however volatile Atomicity is not guaranteed .
(3) adopt Lock Interface ensures visibility .
(4) adopt Atomic Type assurance visibility .
(5) adopt final Keywords guarantee visibility
3. Orderliness
That is to say, the sequence of program execution is in accordance with the sequence of code execution .
- But the processor in order to improve the efficiency of the program , The input code may be optimized , It does not guarantee that the execution sequence of each statement in the program is the same as that in the code , But it will ensure that the final execution result of the program is consistent with the sequential execution result of the code . for example :
int i=1;// sentence 1
int j=2;// sentence 2
When executed , It is possible to reorder the code , For example, execute the statement first 2, Then execute the statement 1. But if the code , Programming the following :
int i=1;
int j=2;
j++;// sentence 3
i=j+1;// sentence 4
At this time , sentence 3 And statement 4 There will be no reordering . Because of the statement 3 and 4 There's a dependency , Reordering will affect the results .
But in the case of multithreading , Could go wrong . for example :
boolean flag=false;
private int i=1;
// Threads 1
i += 1;// sentence 1
flag=true;// sentence 2
// Threads 2
if(flag){
j += i;
}
If the thread 1 When it comes to execution , sentence 1 And statement 2 Reordered , Execute statement first 2, Before executing the statement 1 when , When a thread 2 Will be implemented if, Then it will enter if In the block , Now j The value of will go wrong . - How to ensure the order
(1) adopt synchronized Keyword through a variable, only one thread is allowed to execute it at the same time lock Operation to ensure the order of its thread access .
(2) It can be used volatile keyword , The instruction rearrangement operation of the variable modified by it is prohibited . It is the prohibition of instruction reordering through the memory barrier .
边栏推荐
- ArrayList of novice source code
- pytest 用例执行顺序
- 【转载】多元高斯分布(The Multivariate normal distribution)
- [学习进度]5月
- How the ThreadPoolExecutor performs tasks
- Bash shell learning notes (II)
- Traversal recursion + iteration of binary tree
- [reprint] the multivariate normal distribution
- 菜鸟看源码之SparseArray
- ISO 639:1988 : Code for the representation of names of languages
猜你喜欢

Newbie sees the source code arraydeque

After 4 months of job search and 15 interviews, I finally got 3 offers, ranking P7+

Pyqt5 rapid development and practice 3.1 QT designer quick start

MySQL锁机制
![[development tool] ieda red](/img/2d/eec1f74c33ff21ae4951eae44b9369.png)
[development tool] ieda red

Scrapy shell出现的一个错误

Bash shell learning notes (4)

LinkedList of source code

Can SAIC mingjue get out of the haze if its products are unable to sell and decline

菜鸟看源码之ArrayList
随机推荐
Definition and use of C language namespace
Simple use of MFC multithreading
Introduction to authoringrealm
Access rights - private, public, protected
HCI 接口
Pre post pytest method
Synchronized与ReentrantLock
Pyqt5 rapid development and practice Chapter 1 understanding pyqt5
实时流式协议--RTSP
leetcode-209. 长度最小的子数组(二分、前缀和、滑动窗口)
如何配置JdbcRealm数据源呢?
Visual conversion of nmap vulnerability scanning results
Pytest case execution sequence
easyui02
新来个技术总监要我做一个 IP 属地功能~
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
Simple use of MySQL database
easyui03
[学习进度]5月
What does it mean that the configuration file ends in RC