当前位置:网站首页>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
边栏推荐
猜你喜欢

Quantitative calculation research

2.7 overview of livedata knowledge points

Socket TCP for network communication (I)

Shardingsphere sub database and sub table < 3 >

Visual studio 2022 downloading and configuring opencv4.5.5

Integer int compare size

Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!

MCDF Experiment 1

Vulnhub's Nagini

Download address and installation tutorial of vs2015
随机推荐
Flutter 退出登录二次确认怎么做才更优雅?
Introduction to concurrent programming (I)
PHP导出word方法(一phpword)
【mysql专项】读锁和写锁
2020-09_ Shell Programming Notes
Lambda表达式
2.7 overview of livedata knowledge points
[official MySQL document] deadlock
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
1-2 project technology selection and structure
lambda与匿名内部类的区别
regular expression
error: expected reference but got (raw string)
laravel 时区问题timezone
在网上炒股开户可以吗?资金安全吗?
Adult adult adult
Fundamentals of concurrent programming (III)
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
2.6 preliminary cognition of synergetic couroutines