-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathFalseSharing.java
More file actions
53 lines (43 loc) · 1.36 KB
/
Copy pathFalseSharing.java
File metadata and controls
53 lines (43 loc) · 1.36 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
package ch5.s4;
public class FalseSharing implements Runnable {
public final static int NUM_THREADS = 2;
public final static long ITERATIONS = 500L * 1000L * 1000L;
private final int arrayIndex;
private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];
static {
for(int i=0;i<longs.length;i++){
longs[i] = new VolatileLong();
}
}
public FalseSharing(final int arrayIndex) {
this.arrayIndex = arrayIndex;
}
public static void main(final String[] args) throws InterruptedException {
final long start = System.currentTimeMillis();
runTest();
System.out.println("duration = " + (System.currentTimeMillis() - 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();
}
}
@Override
public void run() {
long i = ITERATIONS +1;
while(0!= --i){
longs[arrayIndex].value = i;
}
}
public final static class VolatileLong {
public volatile long value = 0L;
// public long p1, p2, p3, p4, p5, p6, p7;
}
}