当前位置:网站首页>[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 .
边栏推荐
- Unity editor expansion - controls, layouts
- [rust notes] 02 ownership
- Osgearth north arrow display
- Development experience and experience
- 【Rust 笔记】08-枚举与模式
- Mall management system of database application technology course design
- Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
- Kunlunbase meetup is waiting for you!
- Graphics_ Learnopongl learning notes
- [RPC] RPC remote procedure call
猜你喜欢

Visual Studio (VS) shortcut keys

Notes on understanding applets 2022/7/3

Chocolate installation

Collection interface

Unity editor expansion - scrolling list

Servlet的生命周期

Jupyter remote server configuration and server startup

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

MySQL 8
随机推荐
【Rust 笔记】13-迭代器(上)
Message queue for interprocess communication
[cloud native] introduction and use of feign of microservices
Map的实现类的顺序性
UE4 source code reading_ Bone model and animation system_ Animation compression
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Unity multi open script
Collection interface
Servlet的生命周期
单调栈-84. 柱状图中最大的矩形
796 · unlock
Graphics_ Games101/202 learning notes
LinkList
梯度下降法求解BP神经网络的简单Demo
【音视频】ijkplayer错误码
Image processing 8-cnn image classification
Osganimation library parsing
Graphics_ Learnopongl learning notes
Swagger document configuration
Development experience and experience