当前位置:网站首页>[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 .
边栏推荐
- Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
- [updating] wechat applet learning notes_ three
- Mysql容器化(1)Docker安装MySQL
- Visual Studio (VS) shortcut keys
- Redis cluster series 4
- Monotonic stack -503 Next bigger Element II
- 數據庫應用技術課程設計之商城管理系統
- Use of ue5 QRcode plug-in
- Base64编码简介
- 数据分析练习题
猜你喜欢

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

十六进制编码简介
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

animation

Markdown learning
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

Introduction to hexadecimal coding

简易入手《SOM神经网络》的本质与原理
随机推荐
Graphics_ Games101/202 learning notes
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Ue5 opencv plug-in use
Markdown directory generation
P1596 [USACO10OCT]Lake Counting S
[updating] wechat applet learning notes_ three
【更新中】微信小程序学习笔记_3
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Mysql容器化(1)Docker安装MySQL
简易入手《SOM神经网络》的本质与原理
单调栈-42. 接雨水
Collection interface
Mall management system of database application technology course design
【Rust 笔记】11-实用特型
matlab神经网络所有传递函数(激活函数)公式详解
Constraintlayout's constraintset dynamically modifies constraints
二进制转十进制,十进制转二进制
【音视频】ijkplayer错误码
【云原生】微服务之Feign的介绍与使用