当前位置:网站首页>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
边栏推荐
- (构造笔记)MIT reading部分学习心得
- adb push apk
- Introduction to concurrent programming (I)
- 145. Post order traversal of binary tree
- 【附下载】密码获取工具LaZagne安装及使用
- Solve msvcp120d DLL and msvcr120d DLL missing
- Introduction to concurrent programming (II)
- [learning notes] DP status and transfer
- 云计算未来 — 云原生
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
猜你喜欢
随机推荐
Pragma pack syntax and usage
Solution à la défaillance de l'installation d'Electron
PHP export word method (phpword)
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Slf4j log facade
网上炒股开户安不安全?谁给回答一下
Dart: about grpc (I)
【mysql官方文档】死锁
PHP get the file list and folder list under the folder
error: expected reference but got (raw string)
Shutter: about inheritedwidget
temp
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Colleagues wrote a responsibility chain model, with countless bugs
使用BLoC 构建 Flutter的页面实例
Qt+vtk+occt reading iges/step model
Solve msvcp120d DLL and msvcr120d DLL missing
102. Sequence traversal of binary tree
lambda与匿名内部类的区别
(construction notes) ADT and OOP