-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFalseSharing.java
More file actions
109 lines (92 loc) · 3.23 KB
/
FalseSharing.java
File metadata and controls
109 lines (92 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.test.multithread;
import java.util.Comparator;
public class FalseSharing {
public static int NUM_THREAD_MAX = 4;
public final static long ITERATIONS = 50_000_0000L;
private static VolatileLongPadded[] paddedLongs;
private static VolatileLongUnPadded[] unPaddedLongs;
public final static class VolatileLongPadded {
public long q1, q2, q3, q4, q5, q6;
public volatile long value = 0L;
public long q11, q12, q13, q14, q15, q16;
}
public final static class VolatileLongUnPadded {
public volatile long value = 0L;
}
static {
paddedLongs = new VolatileLongPadded[NUM_THREAD_MAX];
for (int i = 0; i < paddedLongs.length; i++) {
paddedLongs[i] = new VolatileLongPadded();
}
unPaddedLongs = new VolatileLongUnPadded[NUM_THREAD_MAX];
for (int i = 0; i < paddedLongs.length; i++) {
unPaddedLongs[i] = new VolatileLongUnPadded();
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
runBenchMark();
}
private static void runBenchMark() throws InterruptedException {
long begin, end;
for (int n = 1; n <= NUM_THREAD_MAX; n++) {
Thread[] threads = new Thread[n];
for (int j = 0; j < threads.length; j++) {
threads[j] = new Thread(createPaddedRunnable(j));
}
begin = System.currentTimeMillis();
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
end = System.currentTimeMillis();
System.out.printf(" Padded # threads %d - T = %dms\n", n, end - begin);
for(VolatileLongPadded padded : paddedLongs) {
System.out.println(padded.value);
}
for (int j = 0; j < threads.length; j++) {
threads[j] = new Thread(createUnPaddedRunnable(j));
}
begin = System.currentTimeMillis();
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
end = System.currentTimeMillis();
System.out.printf(" UnPadded # threads %d - T = %dms\n", n, end - begin);
for(VolatileLongPadded padded : paddedLongs) {
System.out.println(padded.value);
}
}
}
private static Runnable createUnPaddedRunnable(int j) {
return () -> {
long i = ITERATIONS + 1;
while (0 != --i) {
unPaddedLongs[j].value = i;
}
};
}
private static Runnable createPaddedRunnable(int j) {
return () -> {
long i = ITERATIONS + 1;
while (0 != --i) {
paddedLongs[j].value = i;
}
};
}
}
/*
* Padded # threads 1 - T = 3652ms
UnPadded # threads 1 - T = 3686ms
Padded # threads 2 - T = 4221ms
UnPadded # threads 2 - T = 14164ms
Padded # threads 3 - T = 5836ms
UnPadded # threads 3 - T = 12517ms
Padded # threads 4 - T = 7800ms
UnPadded # threads 4 - T = 13013ms
*/