当前位置:网站首页>慢慢学JVM之缓存行和伪共享
慢慢学JVM之缓存行和伪共享
2022-06-26 05:17:00 【青铜大神】
缓存行(Cache Line)
缓存行是CPU缓存的最小单位,CPU的缓存是由多个缓存行组成的,一个缓存行通常是64字节大小。所以一个缓存行可以存储8个long类型的变量。
CPU缓存的工作流程是每次访问缓存,如果缓存命中,会把整个缓存行读取出来进行修改。这种机制就会产生伪共享问题。
伪共享问题
伪共享问题产生在多线程环境下,线程A与线程B同时修改位于同一缓存行内的不同数据,导致缓存失效。它们会互相覆盖导致频繁的缓存未命中,
public class FalseShareTest implements Runnable {
// 并发线程数:4
public static int NUM_THREADS = 4;
// 迭代次数:100万次
public final static long ITERATIONS = 1_000_000L;
// 数组索引数
private final int arrayIndex;
// VolatileLong对象数组
private static VolatileLong[] longs;
// 花费总时长
public static long SUM_TIME = 0l;
public FalseShareTest(final int arrayIndex) {
this.arrayIndex = arrayIndex;
}
private static void runTest() throws InterruptedException {
Thread[] threads = new Thread[NUM_THREADS];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new FalseShareTest(i));
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
}
// 对对象数组进行修改
public void run() {
long i = ITERATIONS + 1;
while (0 != --i) {
longs[arrayIndex].value = i;
}
}
public final static class VolatileLong {
// 加volatile让变量的修改对所有线程可见
public volatile long value = 0L;
}
public static void main(final String[] args) throws Exception {
// 执行10次
for (int j = 0; j < 10; j++) {
// 构建实验对象数组
longs = new VolatileLong[NUM_THREADS];
for (int i = 0; i < longs.length; i++) {
longs[i] = new VolatileLong();
}
// 开始时间戳
final long start = System.currentTimeMillis();
// 运行测试程序
runTest();
// 结束时间戳
final long end = System.currentTimeMillis();
SUM_TIME += end - start;
}
System.out.println("总耗时:" + SUM_TIME);
}
}运行结果

如何改进?
第一种办法,由于一个缓存行可以存储64个字节,也就是8个long型变量,那我就前后各安插7各long型变量,让字段value,前不着村,后不着店。这也是高性能队列Disruptoer的解决方式。
public final static class VolatileLong {
// 填充
public long p1, p2, p3, p4, p5;
// 加volatile让变量的修改对所有线程可见
public volatile long value = 0L;
// 填充
public long p6, p7, p8, p9, p10;
}这里存在一个细节,一个long型变量在32位操作系统种占用8个字节,64位操作系统种占用16个字节,也就是说我偶们只需要5个long变量就能占满64个字节
第二种办法,使用JDK8新增的@Contented,使用@Contented注解后会增加128字节的padding,需要启动时增加-XX:-RestrictContented选项才能生效。
边栏推荐
- Codeforces Round #802 (Div. 2)(A-D)
- Briefly describe the pitfalls of mobile IM development: architecture design, communication protocol and client
- PHP one sentence Trojan horse
- 2. < tag dynamic programming and conventional problems > lt.343 integer partition
- Zuul 实现动态路由
- Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
- Excellent learning ability is your only sustainable competitive advantage
- 出色的学习能力,才是你唯一可持续的竞争优势
- 【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
- Guanghetong and anti international bring 5g R16 powerful performance to the AI edge computing platform based on NVIDIA Jetson Xavier nx
猜你喜欢

SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor

红队得分方法统计

Tp5.0 framework PDO connection MySQL error: too many connections solution

Zuul 實現動態路由

Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法

Tensorflow and deep learning day 3

6.1 - 6.2 公鑰密碼學簡介
![C# 39. Conversion between string type and byte[] type (actual measurement)](/img/33/046aef4e0c1d7c0c0d60c28e707546.png)
C# 39. Conversion between string type and byte[] type (actual measurement)

The first gift of the project, the flying oar contract!

How to select the data transmission format of instant messaging application
随机推荐
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
SSH connected to win10 and reported an error: permission denied (publickey, keyboard interactive)
Machine learning final exercises
Tp5.0 framework PDO connection MySQL error: too many connections solution
One of token passing between microservices @feign's token passing
Create a binary response variable using the cut sub box operation
[leetcode] 713: subarray with product less than k
ThreadPoolExecutor实现文件上传批量插入数据
The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
Cookie and session Basics
ssh连win10报错:Permission denied (publickey,keyboard-interactive).
[red team] what preparations should be made to join the red team?
Chapter 9 setting up structured logging (I)
Create SSH key pair configuration steps
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
Tensorflow and deep learning day 3
Using requests library and re library to crawl web pages
百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
瀚高数据库自定义操作符‘!~~‘