1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.2 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
jsr166 |
1.6 |
import java.util.*; |
8 |
dl |
1.1 |
import java.util.concurrent.*; |
9 |
|
|
import java.util.concurrent.locks.*; |
10 |
|
|
|
11 |
|
|
public final class LockOncePerThreadLoops { |
12 |
|
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
13 |
|
|
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
14 |
|
|
static boolean print = false; |
15 |
|
|
static int nlocks = 500000; |
16 |
|
|
static int nthreads = 100; |
17 |
|
|
static int replications = 20; |
18 |
|
|
|
19 |
|
|
public static void main(String[] args) throws Exception { |
20 |
jsr166 |
1.3 |
if (args.length > 0) |
21 |
dl |
1.1 |
replications = Integer.parseInt(args[0]); |
22 |
|
|
|
23 |
jsr166 |
1.3 |
if (args.length > 1) |
24 |
dl |
1.1 |
nlocks = Integer.parseInt(args[1]); |
25 |
|
|
|
26 |
|
|
print = true; |
27 |
|
|
|
28 |
|
|
for (int i = 0; i < replications; ++i) { |
29 |
|
|
System.out.print("Iteration: " + i); |
30 |
|
|
new ReentrantLockLoop().test(); |
31 |
|
|
Thread.sleep(100); |
32 |
|
|
} |
33 |
|
|
pool.shutdown(); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
static final class ReentrantLockLoop implements Runnable { |
37 |
|
|
private int v = rng.next(); |
38 |
|
|
private volatile int result = 17; |
39 |
|
|
final ReentrantLock[]locks = new ReentrantLock[nlocks]; |
40 |
|
|
|
41 |
|
|
private final ReentrantLock lock = new ReentrantLock(); |
42 |
|
|
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
43 |
|
|
private final CyclicBarrier barrier; |
44 |
|
|
ReentrantLockLoop() { |
45 |
|
|
barrier = new CyclicBarrier(nthreads+1, timer); |
46 |
|
|
for (int i = 0; i < nlocks; ++i) |
47 |
|
|
locks[i] = new ReentrantLock(); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
final void test() throws Exception { |
51 |
jsr166 |
1.3 |
for (int i = 0; i < nthreads; ++i) |
52 |
dl |
1.1 |
pool.execute(this); |
53 |
|
|
barrier.await(); |
54 |
|
|
barrier.await(); |
55 |
|
|
if (print) { |
56 |
|
|
long time = timer.getTime(); |
57 |
jsr166 |
1.4 |
double secs = (double) time / 1000000000.0; |
58 |
dl |
1.1 |
System.out.println("\t " + secs + "s run time"); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
int r = result; |
62 |
|
|
if (r == 0) // avoid overoptimization |
63 |
|
|
System.out.println("useless result: " + r); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
public final void run() { |
67 |
|
|
try { |
68 |
jsr166 |
1.3 |
barrier.await(); |
69 |
dl |
1.1 |
int sum = v; |
70 |
|
|
int x = 0; |
71 |
|
|
for (int i = 0; i < locks.length; ++i) { |
72 |
|
|
locks[i].lock(); |
73 |
|
|
try { |
74 |
|
|
v = x += ~(v - i); |
75 |
|
|
} |
76 |
|
|
finally { |
77 |
|
|
locks[i].unlock(); |
78 |
|
|
} |
79 |
|
|
// Once in a while, do something more expensive |
80 |
|
|
if ((~i & 255) == 0) { |
81 |
|
|
sum += LoopHelpers.compute1(LoopHelpers.compute2(x)); |
82 |
|
|
} |
83 |
|
|
else |
84 |
|
|
sum += sum ^ x; |
85 |
|
|
} |
86 |
|
|
barrier.await(); |
87 |
|
|
result += sum; |
88 |
|
|
} |
89 |
jsr166 |
1.3 |
catch (Exception ie) { |
90 |
|
|
return; |
91 |
dl |
1.1 |
} |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
} |