当前位置:网站首页>[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 .
边栏推荐
- [concurrent programming] concurrent tool class of thread
- 单调栈-42. 接雨水
- Easy touch plug-in
- How to deal with the core task delay caused by insufficient data warehouse resources
- Intersectionpicker in osgearth
- [audio and video] ijkplayer error code
- [rust note] 10 operator overloading
- UE4 call DLL
- Unity Editor Extension - Outline
- UE4 source code reading_ Bone model and animation system_ Animation compression
猜你喜欢
Introduction to hexadecimal coding
UE4 source code reading_ Mobile synchronization
数据分析练习题
[rust notes] 02 ownership
Unity editor expansion - controls, layouts
php-fpm软件的安装+openresty高速缓存搭建
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Mall management system of database application technology course design
Redis data structure
随机推荐
Encoding and decoding of golang URL
Jupyter remote server configuration and server startup
Unity notes 1
【Rust 笔记】13-迭代器(上)
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Pit & ADB wireless debugging of vivo real machine debugging
796 · 开锁
十六进制编码简介
Why can void * be a general pointer
[concurrent programming] working mechanism and type of thread pool
[cloud native] introduction and use of feign of microservices
Sequence of map implementation classes
Some understandings of 3dfiles
【Rust 笔记】10-操作符重载
Map的实现类的顺序性
Advanced OSG collision detection
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Golang's range
Find the intersection of line segments
单调栈-84. 柱状图中最大的矩形