当前位置:网站首页>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
边栏推荐
- 2.6 preliminary cognition of synergetic couroutines
- 2020-11_ Technical experience set
- During FTP login, the error "530 login incorrect.login failed" is reported
- 239. Sliding window maximum
- 【嵌入式】---- 内存四区介绍
- If you can't learn, you have to learn. Jetpack compose writes an im app (II)
- 为什么我的mysql容器启动不了呢
- Official website of Unicode query
- 242. Effective letter heteronyms
- Fluent: Engine Architecture
猜你喜欢
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

Shardingsphere sub database and sub table < 3 >

2.7 overview of livedata knowledge points
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock

Talk about the state management mechanism in Flink framework

4000字超详解指针

PHP導出word方法(一mht)

PHP export word method (phpword)

C language improvement article (wchar_t) character type

1-2 project technology selection and structure
随机推荐
JVM内存模型
Solution à la défaillance de l'installation d'Electron
PHP導出word方法(一mht)
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
网络通讯之Socket-Tcp(一)
Wechat applet - basic content
(构造笔记)ADT与OOP
During FTP login, the error "530 login incorrect.login failed" is reported
New features of ES6
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
(construction notes) learning experience of MIT reading
实现验证码验证
[MySQL special] read lock and write lock
Interview experience in summer camp of Central South University in 2022
MCDF Experiment 1
Introduction to concurrent programming (I)
Shutter: overview of shutter architecture (excerpt)
Jsup crawls Baidu Encyclopedia
Dart: view the dill compiled code file
pragma-pack语法与使用