False Sharing && Java 7

本文探讨了Java 7版本中虚假共享的问题,并通过实验代码展示了如何使用填充字段来避免这一现象,旨在提升并发编程环境下的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://mechanical-sympathy.blogspot.hk/2011/08/false-sharing-java-7.html (因为被墙移动到墙内)

In my previous post on False Sharing 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 False Sharing 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值