1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
// Yet another contended object monitor throughput test |
8 |
// adapted from bug reports |
9 |
|
10 |
import java.util.*; |
11 |
import java.lang.*; |
12 |
import java.util.concurrent.*; |
13 |
import java.util.concurrent.locks.*; |
14 |
|
15 |
class Producer extends Thread |
16 |
{ |
17 |
// private static Hashtable buddiesOnline = new Hashtable(); |
18 |
private static Map buddiesOnline = new ConcurrentHashMap(); |
19 |
public Producer(String name) { super(name); } |
20 |
|
21 |
public void run() { |
22 |
Object key = null; |
23 |
final ReentrantLock dr = RLJBar.DeathRow; |
24 |
final ReentrantLock bar = RLJBar.bar; |
25 |
final ReentrantLock end = RLJBar.End; |
26 |
final Condition endCondition = RLJBar.EndCondition; |
27 |
if (RLJBar.OneKey) key = new Integer(0); // per-thread v. per iteration |
28 |
|
29 |
// The barrier has a number of interesting effects: |
30 |
// 1. It enforces full LWP provisioning on T1. |
31 |
// (nearly all workers park concurrently). |
32 |
// 2. It gives the C2 compiler thread(s) a chance to run. |
33 |
// By transiently quiescing the workings the C2 threads |
34 |
// might avoid starvation. |
35 |
// |
36 |
|
37 |
try { |
38 |
bar.lock(); |
39 |
try { |
40 |
++RLJBar.nUp; |
41 |
if (RLJBar.nUp == RLJBar.nThreads) { |
42 |
if (RLJBar.quiesce != 0) { |
43 |
RLJBar.barCondition.awaitNanos(RLJBar.quiesce * 1000000); |
44 |
} |
45 |
RLJBar.epoch = System.currentTimeMillis(); |
46 |
RLJBar.barCondition.signalAll(); |
47 |
// System.out.print ("Consensus "); |
48 |
} |
49 |
if (RLJBar.UseBar) { |
50 |
while (RLJBar.nUp != RLJBar.nThreads) { |
51 |
RLJBar.barCondition.await(); |
52 |
} |
53 |
} |
54 |
} |
55 |
finally { |
56 |
bar.unlock(); |
57 |
} |
58 |
} catch (Exception ex) { |
59 |
System.out.println("Exception in barrier: " + ex); |
60 |
} |
61 |
|
62 |
// Main execution time ... the code being timed ... |
63 |
// HashTable.get() is highly contended (serial). |
64 |
for (int loop = 1; loop < 100000 ;loop++) { |
65 |
if (!RLJBar.OneKey) key = new Integer(0); |
66 |
buddiesOnline.get(key); |
67 |
} |
68 |
|
69 |
// Mutator epilog: |
70 |
// The following code determines if the test will/wont include (measure) |
71 |
// thread death time. |
72 |
|
73 |
end.lock(); |
74 |
try { |
75 |
++RLJBar.nDead; |
76 |
if (RLJBar.nDead == RLJBar.nUp) { |
77 |
// System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms"); |
78 |
endCondition.signalAll(); |
79 |
} |
80 |
} |
81 |
finally { |
82 |
end.unlock(); |
83 |
} |
84 |
dr.lock(); |
85 |
dr.unlock(); |
86 |
} |
87 |
} |
88 |
|
89 |
|
90 |
public class RLJBar // ProdConsTest |
91 |
{ |
92 |
|
93 |
public static final int ITERS = 10; |
94 |
public static boolean OneKey = false; // alloc once or once per iteration |
95 |
|
96 |
public static boolean UseBar = false; |
97 |
public static int nThreads = 100; |
98 |
public static int nUp = 0; |
99 |
public static int nDead = 0; |
100 |
public static ReentrantLock bar = new ReentrantLock(); |
101 |
public static Condition barCondition = bar.newCondition(); |
102 |
public static long epoch; |
103 |
public static ReentrantLock DeathRow = new ReentrantLock(); |
104 |
public static ReentrantLock End = new ReentrantLock(); |
105 |
public static int quiesce = 0; |
106 |
public static Condition EndCondition = End.newCondition(); |
107 |
|
108 |
public static void main(String[] args) { |
109 |
int argix = 0; |
110 |
if (argix < args.length && args[argix].equals("-o")) { |
111 |
++argix; |
112 |
OneKey = true; |
113 |
System.out.println("OneKey"); |
114 |
} |
115 |
if (argix < args.length && args[argix].equals ("-b")) { |
116 |
++argix; |
117 |
UseBar = true; |
118 |
System.out.println("UseBar"); |
119 |
} |
120 |
if (argix < args.length && args[argix].equals ("-q")) { |
121 |
++argix; |
122 |
if (argix < args.length) { |
123 |
quiesce = Integer.parseInt(args[argix++]); |
124 |
System.out.println("Quiesce " + quiesce + " msecs"); |
125 |
} |
126 |
} |
127 |
for (int k = 0; k < ITERS; ++k) |
128 |
oneRun(); |
129 |
} |
130 |
|
131 |
public static void oneRun() { |
132 |
DeathRow = new ReentrantLock(); |
133 |
End = new ReentrantLock(); |
134 |
EndCondition = End.newCondition(); |
135 |
|
136 |
nDead = nUp = 0; |
137 |
long cyBase = System.currentTimeMillis(); |
138 |
DeathRow.lock(); |
139 |
try { |
140 |
for (int i = 1; i <= nThreads; i++) { |
141 |
new Producer("Producer" + i).start(); |
142 |
} |
143 |
try { |
144 |
End.lock(); |
145 |
try { |
146 |
while (nDead != nThreads) |
147 |
EndCondition.await(); |
148 |
} |
149 |
finally { |
150 |
End.unlock(); |
151 |
} |
152 |
} catch (Exception ex) { |
153 |
System.out.println("Exception in End: " + ex); |
154 |
} |
155 |
} |
156 |
finally { |
157 |
DeathRow.unlock(); |
158 |
} |
159 |
System.out.println("Outer time: " + (System.currentTimeMillis()-cyBase)); |
160 |
|
161 |
// Let workers quiesce/exit. |
162 |
try { Thread.sleep (1000); } catch (Exception ex) {}; |
163 |
} |
164 |
} |