Created
July 20, 2016 07:53
-
-
Save killme2008/f3fb7b64cabfce2f9d3c047eb041f7a3 to your computer and use it in GitHub Desktop.
Java False sharing test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FalseSharing implements Runnable { | |
public final static int NUM_THREADS = 4; // change | |
public final static long ITERATIONS = 500L * 1000L * 1000L; | |
private final int arrayIndex; | |
private static VolatileLong3[] longs = new VolatileLong3[NUM_THREADS]; | |
static { | |
for (int i = 0; i < longs.length; i++) { | |
longs[i] = new VolatileLong3(); | |
} | |
} | |
public FalseSharing(final int arrayIndex) { | |
this.arrayIndex = arrayIndex; | |
} | |
public static void main(final String[] args) throws Exception { | |
long start = System.nanoTime(); | |
runTest(); | |
System.out.println("duration = " + (System.nanoTime() - start)); | |
} | |
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 FalseSharing(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 { | |
public volatile long value = 0L; | |
} | |
// long padding避免false sharing | |
public final static class VolatileLong2 { | |
volatile long p0, p1, p2, p3, p4, p5, p6; | |
public volatile long value = 0L; | |
volatile long q0, q1, q2, q3, q4, q5, q6; | |
} | |
// jdk8新特性,Contended注解避免false sharing | |
// Restricted on user classpath | |
// Unlock: -XX:-RestrictContended | |
@sun.misc.Contended | |
public final static class VolatileLong3 { | |
public volatile long value = 0L; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment