当前位置:网站首页>[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 .
边栏推荐
- Message queue for interprocess communication
- 单调栈-503. 下一个更大元素 II
- Eating fruit
- Minimap plug-in
- Unity editor expansion - the design idea of imgui
- UE4 source code reading_ Bone model and animation system_ Animation compression
- producer consumer problem
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- Unity learning notes
- [concurrent programming] concurrent tool class of thread
猜你喜欢

Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network

Ue5 opencv plug-in use

二进制转十进制,十进制转二进制

Monotonic stack -503 Next bigger Element II

Some understandings of 3dfiles

Unity interactive water ripple post-treatment

【更新中】微信小程序学习笔记_3

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Image processing 8-cnn image classification

注解简化配置与启动时加载
随机推荐
【Rust 笔记】13-迭代器(上)
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Conversion between string and int types in golang
十六进制编码简介
Development material set
Development experience and experience
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
[concurrent programming] concurrent tool class of thread
[concurrent programming] working mechanism and type of thread pool
redis集群系列四
UE4 source code reading_ Mobile synchronization
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Message pack in C deserializes array objects
MySQL 8
Monotonic stack -503 Next bigger Element II
[concurrent programming] thread foundation and sharing between threads
如何应对数仓资源不足导致的核心任务延迟
Osgconv tool usage
Visual Studio (VS) shortcut keys
[redis] redis persistent RDB vs AOF (source code)