博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
False Sharing && Java 7
阅读量:6716 次
发布时间:2019-06-25

本文共 2397 字,大约阅读时间需要 7 分钟。

原文: (因为被墙移动到墙内)

In my previous post on  I suggested it can be avoided by padding the cache line with unused longfields.  It seems Java 7 got clever and eliminated or re-ordered the unused fields, thus re-introducing false sharing.  I’ve experimented with a number of techniques on different platforms and found the following code to be the most reliable.

01 import java.util.concurrent.atomic.AtomicLong;
02  
03 public final class FalseSharing
04     implements Runnable
05 {
06     public final static int NUM_THREADS = 4; // change
07     public final static long ITERATIONS = 500L * 1000L * 1000L;
08     private final int arrayIndex;
09  
10     private static PaddedAtomicLong[] longs = new PaddedAtomicLong[NUM_THREADS];
11     static
12     {
13         for (int i = 0; i < longs.length; i++)
14         {
15             longs[i] = new PaddedAtomicLong();
16         }
17     }
18  
19     public FalseSharing(final int arrayIndex)
20     {
21         this.arrayIndex = arrayIndex;
22     }
23  
24     public static void main(final String[] args) throws Exception
25     {
26         final long start = System.nanoTime();
27         runTest();
28         System.out.println("duration = " + (System.nanoTime() - start));
29     }
30  
31     private static void runTest() throws InterruptedException
32     {
33         Thread[] threads = new Thread[NUM_THREADS];
34  
35         for (int i = 0; i < threads.length; i++)
36         {
37             threads[i] = new Thread(new FalseSharing(i));
38         }
39  
40         for (Thread t : threads)
41         {
42             t.start();
43         }
44  
45         for (Thread t : threads)
46         {
47             t.join();
48         }
49     }
50  
51     public void run()
52     {
53         long i = ITERATIONS + 1;
54         while (0 != --i)
55         {
56             longs[arrayIndex].set(i);
57         }
58     }
59  
60     public static long sumPaddingToPreventOptimisation(final int index)
61     {
62         PaddedAtomicLong v = longs[index];
63         return v.p1 + v.p2 + v.p3 + v.p4 + v.p5 + v.p6;
64     }
65  
66     public static class PaddedAtomicLong extends AtomicLong
67     {
68         public volatile long p1, p2, p3, p4, p5, p6 = 7L;
69     }
70 }

With this code I get similar performance results to those stated in the previous  article.  The padding inPaddedAtomicLong above can be commented out to see the false sharing effect.

I think we should all lobby the powers that be inside Oracle to have intrinsics added to the language so we can have cache line aligned and padded atomic classes.  This and some other low-level changes would help make Java a real concurrent programming language.  We keep hearing them say multi-core is coming.   I say it is here and Java needs to catch up.

转载地址:http://xarlo.baihongyu.com/

你可能感兴趣的文章
redis 主从配置和集群配置
查看>>
手机3D游戏开发:自定义Joystick的相关设置和脚本源码
查看>>
java 数组偶数排在奇数前面
查看>>
window.frames["detailFrm"].isSubmitting = true;//?起什么作用
查看>>
ASCII表
查看>>
idea之debug
查看>>
什么是真正的流程管理?流程管理的是与不是。
查看>>
SEO实践:SEO友好的URL结构
查看>>
洛谷P1613 跑路
查看>>
python各种模块,迭代器,生成器
查看>>
微信小程序 watch监听数据变化 类似vue中的watch
查看>>
u检验、t检验、F检验、X2检验 (转)
查看>>
不可不知的Python模块: collections
查看>>
PAT 1066. Root of AVL Tree (25)
查看>>
hdu1052
查看>>
服务器端推送技术
查看>>
python开发工具
查看>>
Home Assistant系列 -- 自动语音播报天气
查看>>
Hyberledger-Fabric 1.00 RPC学习(1)
查看>>
SDNU 1450.报时助手
查看>>