当前位置:网站首页>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
边栏推荐
- PHP get the file list and folder list under the folder
- Redis
- Oracle advanced (I) realize DMP by expdp impdp command
- 使用BLoC 构建 Flutter的页面实例
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- SystemVerilog -- OOP -- copy of object
- 2.8 overview of ViewModel knowledge
- 225. Implement stack with queue
- laravel 时区问题timezone
猜你喜欢

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

OpenGL draws colored triangles

Flutter 退出登录二次确认怎么做才更优雅?

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

Shardingsphere sub database and sub table < 3 >

云计算未来 — 云原生

Unicode encoding table download

实现验证码验证

laravel 时区问题timezone

Why can't my MySQL container start
随机推荐
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
JVM内存模型
QT OpenGL texture map
Redis
20. Valid brackets
ES6新特性
Symlink(): solution to protocol error in PHP artisan storage:link on win10
QT OpenGL rotate, pan, zoom
Introduction to concurrent programming (I)
2.8 overview of ViewModel knowledge
Flutter 退出登录二次确认怎么做才更优雅?
225. Implement stack with queue
PHP get the file list and folder list under the folder
Slf4j log facade
Cloud Computing future - native Cloud
[learning notes] DP status and transfer
error: expected reference but got (raw string)
1-1 token
MySQL time zone solution
Shutter: overview of shutter architecture (excerpt)