当前位置:网站首页>01_ Using the concurrent tool class library, is thread safety safe
01_ Using the concurrent tool class library, is thread safety safe
2022-07-03 12:24:00 【jackaroo2020】
I. Using the concurrent tool class library
1. Not aware of the confusion of user information caused by thread reuse Bug
One is , Only know how to use concurrent tools , But I don't know the context of the current thread , Solve multithreading problems without understanding threads . such as , Use ThreadLocal To cache data , think ThreadLocal There is no thread safety problem if threads are isolated , Unexpectedly, thread reuse leads to data string . Please remember , Clean up before the business logic ends ThreadLocal Data in .
2. Using thread safe concurrency tools , It doesn't mean that all thread safety issues have been solved
Two is , Mistakenly thinking that using concurrency tools can solve all thread safety problems , Expect to solve the problem by replacing thread unsafe classes with thread safe classes . such as , Think it used ConcurrentHashMap You can solve the thread safety problem , Failure to lock the composite logic leads to business logic errors . If you want to be in a whole piece of business logic , If the operation of the container is consistent as a whole , Need to be locked .
3. Not fully understand the characteristics of concurrency tools , Thus, it cannot exert its power
The third is , Not fully understand the characteristics of concurrency tools , Still use the new tool in the old way, so it can't play its performance . such as , Used ConcurrentHashMap, However, it did not make full use of the information it provided based on CAS A safe way , Or use locks to implement logic .
4. Failed to recognize the usage scenario of concurrency tools , This leads to performance problems
Fourth, , Didn't understand the applicable scenario of the tool , Using the wrong tools in inappropriate scenarios leads to worse performance . such as , Do not understand CopyOnWriteArrayList The applicable scenarios of , Use it in scenarios of read-write balancing or massive write operations , Cause performance problems . For this scenario , You can consider using ordinary List.
II. Supplementary knowledge
1.ThreadLocalRandom, Do you think you can set its instance to static variables , Reuse in the case of multithreading ?
Can not be .ThreadLocalRandom Class only encapsulates some common methods , Seeds are stored in threads .ThreadLocalRandom Store a singleton in instance, call current() Method returns this instance, Each thread calls for the first time current() When the method is used , Will initialize in each thread seed and probe.nextX() Method will call nextSeed(), Use the seeds in each thread , Calculate the next seed and save . therefore , If you use static variables , Call directly nextX() Method skips the initialization steps of each thread , Only in every call nextSeed() Update seeds when .
2. say something ConcurrentHashMap in computeIfAbsent and putIfAbsent Differences in methods ?
a. The parameters are different ,putIfAbsent Is the value ,computeIfAbsent yes mappingFunction
b. The return value is different ,putIfAbsent It's the previous value ,computeIfAbsent Is the current value
3. Different logic of the same thread needs to share data ( But you can't share data by passing values ), Or to avoid creating objects repeatedly by the same thread, you want to reuse data , Consider using ThreadLocal
边栏推荐
- Adult adult adult
- PHP导出word方法(一mht)
- Itext7 uses iexternalsignature container for signature and signature verification
- 1-2 project technology selection and structure
- Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
- 2.9 overview of databinding knowledge points
- Qt+vtk+occt reading iges/step model
- QT OpenGL texture map
- 257. All paths of binary tree
- Adult adult adult
猜你喜欢

Flutter 退出登录二次确认怎么做才更优雅?
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

Talk about the state management mechanism in Flink framework

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

If you can't learn, you have to learn. Jetpack compose writes an im app (I)

Shutter: add gradient stroke to font

Socket TCP for network communication (I)

4000 word super detailed pointer

Qt+vtk+occt reading iges/step model

shardingSphere分库分表<3>
随机推荐
Adult adult adult
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
SystemVerilog -- OOP -- copy of object
Adult adult adult
OpenGL index cache object EBO and lineweight mode
Lambda表达式
1-2 project technology selection and structure
使用BLoC 构建 Flutter的页面实例
(数据库提权——Redis)Redis未授权访问漏洞总结
QT OpenGL rotate, pan, zoom
在网上炒股开户可以吗?资金安全吗?
Interview experience in summer camp of Central South University in 2022
MySQL time zone solution
Shardingsphere sub database and sub table < 3 >
Wechat applet - basic content
Official website of Unicode query
[official MySQL document] deadlock
Qt+vtk+occt reading iges/step model
wpa_ cli