当前位置:网站首页>[concurrent programming] concurrent security
[concurrent programming] concurrent security
2022-07-03 08:37:00 【keeper42】
Thread safety definition of class
If you use this class under multithreading , But how do multithreading use and schedule this class , This class always shows the right behavior , This class is thread safe .
The thread safety performance of class is :
- Atomicity of operation
- Memory visibility
Don't do the right synchronization , When sharing state among multiple threads , There will be thread insecurity .
How to achieve thread safety of class ?
Stack closure
All variables are declared inside the method , These variables are all stack closed .
No state
A class without any member variables , It's called stateless class
Make classes immutable
Make the state immutable , Two ways :
1, Add final keyword , For a class , All member variables should be private , The same as long as possible , All member variables should be added with final keyword , But add final, Note that if the member variable is another object , If the class corresponding to this object is immutable , To ensure that the entire class is immutable .
2、 There is no place to modify member variables at all , At the same time, the member variable is not the return value of the method
volatile
Ensure class visibility , most Suitable for one thread to write , Multiple threads reading Scenario ,
Lock and CAS
Safe release ( return )
Member variables held in class , In particular, references to objects , If this member object is not thread safe , adopt get Wait for the method to be released , This will result in incorrect modification of the data held by the member object itself under multithreading , Thus, the whole class thread is unsafe .
TheadLocal
synchronize (Spring To load a bean Time use synchronize)
HashMap Space for time
ConcurrentHashMap Concurrent
Volatile
Servlet
Not a thread safe class , Why don't we usually feel ,:1、Tomcat Handle ;2、 On demand , There is little need for sharing , second , Request received , When returning the answer , It is the responsibility of one thread .
Deadlock
Two or more processes in execution , A blocking phenomenon caused by competition for resources or communication with each other , If there is no external force , They will not be able to move forward . These processes that have been waiting for each other are called deadlock processes .
Resources must be more than 1 individual , At the same time, it is less than or equal to the number of competing threads , There is only one resource , There will only be fierce competition .
The fundamental cause of deadlock : The inconsistent order in which locks are acquired results in .
ordinary
Suspected sending deadlock :
adopt jps Query applied id,
Re pass jstack id Check the lock holding of the application
terms of settlement : Ensure the sequence of locking
Dynamic
Dynamic sequence deadlock , It is locked in a certain order during implementation , But because of the problem of external calls , Resulting in no guarantee of lock sequence .
solve :
- By internal ordering , Ensure the sequence of locking
- By trying to get the lock , It's fine too .
Other safety issues
Live lock
Try to take the lock mechanism , There are multiple threads giving way to each other , It's going to happen all the time , The process of releasing the lock .
terms of settlement : Sleep random number per thread , Stagger the lock time .
Thread starvation
stay CPU In a busy situation , Low priority threads have little chance of execution , It's possible that threads “ hunger ”; Thread holding lock , If the execution time is too long , May also lead to “ hunger ” problem .
solve “ hunger ” The solution to the problem is simple , There are three options : First, ensure sufficient resources , The second is to allocate resources fairly , The third is to avoid long-time execution of the thread holding the lock . Of the three options , The applicable scenarios of scheme 1 and scheme 3 are relatively limited , Because in many scenes , There is no way to solve the scarcity of resources , It is also difficult to shorten the execution time of the thread holding the lock . However, the applicable scenarios of scheme 2 are relatively more .
边栏推荐
- Development material set
- Introduction to Base64 coding
- Minimap plug-in
- Delete the last character of the string in golang
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- Gradle's method of dynamically modifying APK package name
- 使用base64编码传图片
- [rust notes] 09- special types and generics
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- P1596 [USACO10OCT]Lake Counting S
猜你喜欢
Minimap plug-in
[rust notes] 02 ownership
二进制转十进制,十进制转二进制
[concurrent programming] consistency hash
Introduction to Base64 coding
单调栈-503. 下一个更大元素 II
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Clion toolchains are not configured configure disable profile problem solving
Unity editor expansion - controls, layouts
请求参数的发送和接收
随机推荐
【Rust 笔记】08-枚举与模式
十六进制编码简介
Minimap plug-in
Unity editor expansion - controls, layouts
How to deal with the core task delay caused by insufficient data warehouse resources
[linear table] basic operation of bidirectional linked list specify node exchange
梯度下降法求解BP神经网络的简单Demo
Clion toolchains are not configured configure disable profile problem solving
Downward compatibility and upward compatibility
Unity editor expansion - the framework and context of unity imgui
Redis data structure
如何应对数仓资源不足导致的核心任务延迟
Cesium for unreal quick start - simple scenario configuration
Golang string segmentation, substitution and interception
Notes on understanding applets 2022/7/3
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Golang time format sorting
二进制转十进制,十进制转二进制
Base64编码简介
Development material set