forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContendedTest.java
More file actions
56 lines (51 loc) · 1.57 KB
/
Copy pathContendedTest.java
File metadata and controls
56 lines (51 loc) · 1.57 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
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
public class ContendedTest extends Thread {
private static class DataHolder {
private volatile long l1 = 0;
private volatile long l2 = 0;
private volatile long l3 = 0;
private volatile long l4 = 0;
}
private static DataHolder dh = new DataHolder();
private static long nLoops;
public ContendedTest(Runnable r) {
super(r);
}
public static void main(String[] args) throws Exception {
nLoops = Long.parseLong(args[0]);
int nThreads = Integer.parseInt(args[1]);
ContendedTest[] tests = new ContendedTest[4];
tests[0] = new ContendedTest(() -> {
for (long i = 0; i < nLoops; i++) {
dh.l1 += i;
}
});
tests[1] = new ContendedTest(() -> {
for (long i = 0; i < nLoops; i++) {
dh.l2 += i;
}
});
tests[2] = new ContendedTest(() -> {
for (long i = 0; i < nLoops; i++) {
dh.l3 += i;
}
});
tests[3] = new ContendedTest(() -> {
for (long i = 0; i < nLoops; i++) {
dh.l4 += i;
}
});
long then = System.currentTimeMillis();
for (int i = 0; i < nThreads; i++) {
tests[i].start();
}
for (int i = 0; i < nThreads; i++) {
tests[i].join();
}
long now = System.currentTimeMillis();
System.out.println("Duration: " + (now - then) + " ms");
}
}