-
Notifications
You must be signed in to change notification settings - Fork 1
/
FalseSharingTests.java
140 lines (140 loc) · 3.78 KB
/
FalseSharingTests.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//package org.gy.demo.disruptor;
//
//import java.lang.reflect.Array;
//import org.junit.jupiter.api.Test;
//import sun.misc.Contended;
//
///**
// * 功能描述:
// *
// * @author gy
// * @version 1.0.0
// * @date 2022/6/16 20:37
// */
//public class FalseSharingTests {
//
//
// @Test
// public void testWithPadding() throws Exception {
// execute(10, ValuePadding.class, "ValuePadding");
// }
//
// @Test
// public void testWithNoPadding() throws Exception {
// execute(10, ValueNoPadding.class, "ValueNoPadding");
// }
//
// //JVM 添加 -XX:-RestrictContended 参数后 @sun.misc.Contended 注解才有效
// @Test
// public void testWithContended() throws Exception {
// execute(10, ValueContended.class, "ValueContended");
// }
//
// public static <T extends ValueHolder> void execute(int threadNum, Class<T> clazz, String desc) throws Exception {
// for (int i = 1; i < threadNum; i++) {
// System.gc();
// final long start = System.currentTimeMillis();
// runTest(i, clazz);
// System.out.println(desc + ": Thread num " + i + " duration = " + (System.currentTimeMillis() - start));
// }
// }
//
// public static <T extends ValueHolder> void runTest(int threadNum, Class<T> clazz) throws Exception {
// T[] values = (T[]) Array.newInstance(clazz, threadNum);
// for (int i = 0; i < values.length; i++) {
// values[i] = clazz.newInstance();
// }
// Thread[] threads = new Thread[threadNum];
// for (int i = 0; i < threads.length; i++) {
// threads[i] = new Thread(new FalseSharing<T>(i, values));
// }
// for (Thread t : threads) {
// t.start();
// }
// for (Thread t : threads) {
// t.join();
// }
// }
//
//
// public static class FalseSharing<T extends ValueHolder> implements Runnable {
//
// private final static long ITERATIONS = 500L * 1000L * 100L;
// private int arrayIndex = 0;
//
// private T[] values;
//
// public FalseSharing(final int arrayIndex, final T[] values) {
// this.arrayIndex = arrayIndex;
// this.values = values;
// }
//
// @Override
// public void run() {
// long i = ITERATIONS + 1;
// while (0 != --i) {
// values[arrayIndex].setValue(0);
// }
// }
//
// }
//
// public final static class ValuePadding implements ValueHolder {
//
// protected long p1, p2, p3, p4, p5, p6, p7;
// protected volatile long value = 0L;
// protected long p9, p10, p11, p12, p13, p14, p15;
//
// @Override
// public long getValue() {
// return value;
// }
//
// @Override
// public void setValue(long value) {
// this.value = value;
// }
// }
//
// public final static class ValueContended implements ValueHolder {
//
// @Contended
// protected volatile long value = 0L;
//
// @Override
// public long getValue() {
// return value;
// }
//
// @Override
// public void setValue(long value) {
// this.value = value;
// }
// }
//
// public final static class ValueNoPadding implements ValueHolder {
//
// // protected long p1, p2, p3, p4, p5, p6, p7;
// protected volatile long value = 0L;
// // protected long p9, p10, p11, p12, p13, p14, p15;
//
// @Override
// public long getValue() {
// return value;
// }
//
// @Override
// public void setValue(long value) {
// this.value = value;
// }
// }
//
// interface ValueHolder {
//
// long getValue();
//
// void setValue(long value);
// }
//
//
//}