当前位置:网站首页>性能优化之@Contended减少伪共享
性能优化之@Contended减少伪共享
2022-07-17 14:17:00 【西魏陶渊明】

作者: 西魏陶渊明
博客: https://blog.springlearn.cn/
西魏陶渊明
莫笑少年江湖梦,谁不少年梦江湖
一、什么叫伪共享
说到伪共享,就要说CPU缓存,我们程序执行时候信息会被保存到CPU缓存中
而这些缓存中的数据可能被多线程访问,假如一个线程还没处理完,另外一个线程
就对数据进行了修改,就会导致上一个线程发生幻读的情况,比如刚才看到a=1,然后准备a = a+1。
但是还没做,另外一个线程就先将a变成2了。导致了上一个线程计算后本来应该是a = 1 + 1,变成了a = 2 + 1
计算结果就不对了。
那么对于这种情况当然是不允许发生的,解决方案就是当发现另外一个线程更新了共享变量,就会把cpu缓存中的数据给失效。
然后都重新读取最新的变量值。
这里有一个前提是共享变量,因为两个线程都会用到a,所以a是共享变量。
那么我们在聊伪共享就简单了,下面举一个伪共享变量的例子。
public class ContendedTest {
volatile long a;
volatile long b;
@Test
public void test() throws Exception {
ContendedTest c = new ContendedTest();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 10000_0000L; i++) {
c.a = i;
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 10000_0000L; i++) {
c.b = i;
}
});
final long start = System.nanoTime();
thread1.start();
thread2.start();
thread1.join();
thread2.join();
// 1933
System.out.println((System.nanoTime() - start) / 100_0000);
}
}
两个线程分别来更新a和b属性,根据缓存失效的原理,因为a和b都在同一个对象中,当一个属性被更新,就会触发cpu缓存失效。
那么等于这种情况cpu缓存就没什么用了。我们思考下两个线程分别更新a和b,而a和b没有任何关系。那么a和b是共享变量吗?
当然不是,这就叫伪共享。
二、主动告诉程序伪共享
我们可以使用 @Contended 来声明伪共享变量,从而是cpu不更新缓存。
本地测试时候记得加上jvm参数 -XX:-RestrictContended,否则无效哦。
public class ContendedTest {
@Contended
volatile int a;
@Contended
volatile int b;
@Test
public void test() throws Exception {
ContendedTest c = new ContendedTest();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 10000_0000L; i++) {
c.a = i;
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 10000_0000L; i++) {
c.b = i;
}
});
final long start = System.nanoTime();
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println((System.nanoTime() - start) / 100_0000);
}
}
那么你猜下性能能提高多少呢? 前者1933后者758ms,差不多2.5倍的样子。
那么留下一个问题? 有多少场景都在使用@Contended呢? 知道的请留言评论。
边栏推荐
- NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
- Google Earth Engine——Hansen Global Forest Change v1.8 (2000-2020) 森林覆盖度和森林损失量数据集
- 树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
- Integrated network architecture and network slicing technology of air, earth and sea
- XSS. haozi. Me brush questions
- 虚拟化排错概论
- Configuration of vscode+unity3d
- web安全入门-部署Snort开源IDS/IPS系统
- SSM uses POI to export data to excel
- MySQL autoincrement ID, UUID and snowflake ID
猜你喜欢

The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)

Second classification learning is extended to multi classification learning

Unity3d 模型中心点的转换(源代码)

PowerCLI 脚本性能优化

antd 下拉多选传值到后台做查询操作

Detailed explanation of multiple linear regression

Scala's dosing in idea

vulnhub inclusiveness: 1

LeetCode 2249. Count the number of grid points in the circle

How does unity3d use the asset store to download some useful resource packages
随机推荐
Pytoch realizes multi-layer perceptron manually
Getting started with web security - deploy snort open source ids/ips system
Unity3d 读取mpu9250 例子原代码
Svn learning
Mysql 自增id、uuid与雪花id
What should I do if I can't see any tiles on SAP Fiori launchpad?
Nombre d'entrées nombre d'entrées numériques pures limite de longueur maximale
ENVI_ Idl: use the inverse distance weight method to select the nearest n points for interpolation (bottom implementation) and output them to GeoTIFF format (the effect is equivalent to the inverse di
Satellite network capacity improvement method based on network coding
Google Earth Engine——Hansen Global Forest Change v1.8 (2000-2020) 森林覆盖度和森林损失量数据集
LeetCode 2249. Count the number of grid points in the circle
A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記
To get to the bottom: Principle Analysis of Objective-C correlation attribute
LeetCode 2325. Decrypt message (map)
Deep learning for generic object detection: a survey
Introduction to sap appgyver
[in vivado middle note ILA IP core]
Un modèle de détection par défaut basé sur le réseau neuronal évolutif rapide dans le contrôle de la qualité des produits - lire les notes
腾讯云服务器利用镜像部署WordPress个人网站!
Win10的环境变量配置