当前位置:网站首页>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
边栏推荐
- init. RC service failed to start
- lambda与匿名内部类的区别
- Introduction to concurrent programming (I)
- Qt+vtk+occt reading iges/step model
- Fluent: Engine Architecture
- How to deploy web pages to Alibaba cloud
- Summary of development issues
- QT OpenGL rotate, pan, zoom
- Shutter widget: centerslice attribute
- LeetCode 0556.下一个更大元素 III - 4步讲完
猜你喜欢
PHP导出word方法(一phpword)
Pki/ca and digital certificate
During FTP login, the error "530 login incorrect.login failed" is reported
QT OpenGL rotate, pan, zoom
QT OpenGL texture map
Qt+vtk+occt reading iges/step model
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Colleagues wrote a responsibility chain model, with countless bugs
Swagger
The future of cloud computing cloud native
随机推荐
temp
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
1-1 token
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
实现验证码验证
Shutter: add gradient stroke to font
Atomic atomic operation
Differences between MySQL Union and union all
previous permutation lintcode51
Dart: about grpc (I)
php 获取文件夹下面的文件列表和文件夹列表
网络通讯之Socket-Tcp(一)
PHP导出word方法(一phpword)
Redis notes 01: Introduction
抓包整理外篇fiddler———— 会话栏与过滤器[二]
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
雲計算未來 — 雲原生
DEJA_ Vu3d - cesium feature set 053 underground mode effect
JVM memory model
在网上炒股开户可以吗?资金安全吗?