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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
/* |
7 |
|
* A simple test program. Feel free to play. |
8 |
|
*/ |
9 |
|
|
10 |
+ |
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.util.concurrent.locks.*; |
12 |
– |
import java.util.*; |
13 |
|
|
14 |
|
public final class LockLoops { |
15 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
18 |
|
static boolean doBuiltin = true; |
19 |
|
static boolean doReadWrite = true; |
20 |
|
static boolean doSemaphore = true; |
21 |
< |
static boolean doFair = true; |
21 |
> |
static boolean doStampedLock = true; |
22 |
> |
static boolean doFair = true; |
23 |
|
|
24 |
|
public static void main(String[] args) throws Exception { |
25 |
|
int maxThreads = 100; |
26 |
|
int iters = 1000000; |
27 |
|
int replications = 1; |
28 |
|
|
29 |
< |
if (args.length > 0) |
29 |
> |
if (args.length > 0) |
30 |
|
maxThreads = Integer.parseInt(args[0]); |
31 |
|
|
32 |
< |
if (args.length > 1) |
32 |
> |
if (args.length > 1) |
33 |
|
iters = Integer.parseInt(args[1]); |
34 |
|
|
35 |
< |
if (args.length > 2) |
35 |
> |
if (args.length > 2) |
36 |
|
replications = Integer.parseInt(args[2]); |
37 |
|
|
38 |
|
rng.setSeed(3122688L); |
94 |
|
System.out.print("ReentrantReadWriteLock"); |
95 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
96 |
|
Thread.sleep(10); |
96 |
– |
|
97 |
|
} |
98 |
|
|
99 |
|
if (doSemaphore) { |
106 |
|
System.out.print("FairSemaphore "); |
107 |
|
new FairSemaphoreLoop().test(v, nthreads, iters); |
108 |
|
Thread.sleep(10); |
109 |
– |
|
109 |
|
} |
110 |
|
|
111 |
|
if (doFair) { |
125 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); |
126 |
|
Thread.sleep(10); |
127 |
|
} |
129 |
– |
|
128 |
|
} |
129 |
|
|
130 |
+ |
if (doStampedLock) { |
131 |
+ |
if (print) |
132 |
+ |
System.out.print("StampedLockWrite "); |
133 |
+ |
new StampedLockWriteLoop().test(v, nthreads, iters); |
134 |
+ |
Thread.sleep(10); |
135 |
+ |
|
136 |
+ |
if (print) |
137 |
+ |
System.out.print("StampedLockRead "); |
138 |
+ |
new StampedLockReadLoop().test(v, nthreads, iters); |
139 |
+ |
Thread.sleep(10); |
140 |
+ |
|
141 |
+ |
if (print) |
142 |
+ |
System.out.print("StampedLockReadWrite"); |
143 |
+ |
new StampedLockReadLoop().test(v, nthreads, iters); |
144 |
+ |
Thread.sleep(10); |
145 |
+ |
} |
146 |
|
} |
147 |
|
|
148 |
< |
static abstract class LockLoop implements Runnable { |
148 |
> |
abstract static class LockLoop implements Runnable { |
149 |
|
int v; |
150 |
|
int iters; |
151 |
|
volatile int result; |
156 |
|
v = initialValue; |
157 |
|
this.iters = iters; |
158 |
|
barrier = new CyclicBarrier(nthreads+1, timer); |
159 |
< |
for (int i = 0; i < nthreads; ++i) |
159 |
> |
for (int i = 0; i < nthreads; ++i) |
160 |
|
pool.execute(this); |
161 |
|
barrier.await(); |
162 |
|
barrier.await(); |
164 |
|
if (print) { |
165 |
|
long tpi = time / (iters * nthreads); |
166 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
167 |
< |
// double secs = (double)(time) / 1000000000.0; |
167 |
> |
// double secs = (double) time / 1000000000.0; |
168 |
|
// System.out.print("\t " + secs + "s run time"); |
169 |
|
System.out.println(); |
170 |
|
} |
175 |
|
abstract int loop(int n); |
176 |
|
public final void run() { |
177 |
|
try { |
178 |
< |
barrier.await(); |
178 |
> |
barrier.await(); |
179 |
|
result += loop(iters); |
180 |
|
barrier.await(); |
181 |
|
} |
182 |
< |
catch (Exception ie) { |
183 |
< |
return; |
182 |
> |
catch (Exception ie) { |
183 |
> |
return; |
184 |
|
} |
185 |
|
} |
186 |
|
|
190 |
|
final int loop(int n) { |
191 |
|
int sum = 0; |
192 |
|
while (n-- > 0) { |
193 |
< |
synchronized(this) { |
193 |
> |
synchronized (this) { |
194 |
|
v = LoopHelpers.compute1(v); |
195 |
|
} |
196 |
|
sum += LoopHelpers.compute2(v); |
212 |
|
} |
213 |
|
|
214 |
|
private static class NoLockVolatileLoop extends LockLoop { |
215 |
< |
volatile private int vv; |
215 |
> |
private volatile int vv; |
216 |
|
final int loop(int n) { |
217 |
|
int sum = 0; |
218 |
|
while (n-- > 0) { |
225 |
|
} |
226 |
|
|
227 |
|
private static class ReentrantLockLoop extends LockLoop { |
228 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
228 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
229 |
|
final int loop(int n) { |
230 |
|
int sum = 0; |
231 |
|
while (n-- > 0) { |
243 |
|
} |
244 |
|
|
245 |
|
private static class FairReentrantLockLoop extends LockLoop { |
246 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
246 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
247 |
|
final int loop(int n) { |
248 |
|
int sum = 0; |
249 |
|
while (n-- > 0) { |
261 |
|
} |
262 |
|
|
263 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
264 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
264 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
265 |
|
final int loop(int n) { |
266 |
|
int sum = 0; |
267 |
|
while (n-- > 0) { |
279 |
|
} |
280 |
|
|
281 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
282 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
282 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
283 |
|
final int loop(int n) { |
284 |
|
int sum = 0; |
285 |
|
while (n-- > 0) { |
323 |
|
} |
324 |
|
|
325 |
|
private static class SemaphoreLoop extends LockLoop { |
326 |
< |
final private Semaphore sem = new Semaphore(1, false); |
326 |
> |
private final Semaphore sem = new Semaphore(1, false); |
327 |
|
final int loop(int n) { |
328 |
|
int sum = 0; |
329 |
|
try { |
344 |
|
return sum; |
345 |
|
} |
346 |
|
} |
347 |
+ |
|
348 |
|
private static class FairSemaphoreLoop extends LockLoop { |
349 |
< |
final private Semaphore sem = new Semaphore(1, true); |
349 |
> |
private final Semaphore sem = new Semaphore(1, true); |
350 |
|
final int loop(int n) { |
351 |
|
int sum = 0; |
352 |
|
try { |
369 |
|
} |
370 |
|
|
371 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
372 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
372 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
373 |
|
final int loop(int n) { |
374 |
|
int sum = 0; |
375 |
|
while (n-- > 0) { |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
+ |
private static class StampedLockWriteLoop extends LockLoop { |
398 |
+ |
private final StampedLock lock = new StampedLock(); |
399 |
+ |
final int loop(int n) { |
400 |
+ |
int sum = 0; |
401 |
+ |
while (n-- > 0) { |
402 |
+ |
long stamp = lock.writeLock(); |
403 |
+ |
try { |
404 |
+ |
v = LoopHelpers.compute1(v); |
405 |
+ |
} |
406 |
+ |
finally { |
407 |
+ |
lock.unlockWrite(stamp); |
408 |
+ |
} |
409 |
+ |
sum += LoopHelpers.compute2(v); |
410 |
+ |
} |
411 |
+ |
return sum; |
412 |
+ |
} |
413 |
+ |
} |
414 |
+ |
|
415 |
+ |
private static class StampedLockReadLoop extends LockLoop { |
416 |
+ |
private final StampedLock lock = new StampedLock(); |
417 |
+ |
final int loop(int n) { |
418 |
+ |
int sum = 0; |
419 |
+ |
while (n-- > 0) { |
420 |
+ |
long stamp = lock.readLock(); |
421 |
+ |
try { |
422 |
+ |
v = LoopHelpers.compute1(v); |
423 |
+ |
} |
424 |
+ |
finally { |
425 |
+ |
lock.unlockRead(stamp); |
426 |
+ |
} |
427 |
+ |
sum += LoopHelpers.compute2(v); |
428 |
+ |
} |
429 |
+ |
return sum; |
430 |
+ |
} |
431 |
+ |
} |
432 |
|
|
433 |
+ |
private static class StampedLockReadWriteLoop extends LockLoop { |
434 |
+ |
private final StampedLock lock = new StampedLock(); |
435 |
+ |
final int loop(int n) { |
436 |
+ |
int sum = 0; |
437 |
+ |
while (n-- > 0) { |
438 |
+ |
int x; |
439 |
+ |
long stamp = lock.readLock(); |
440 |
+ |
try { |
441 |
+ |
x = LoopHelpers.compute1(v); |
442 |
+ |
} |
443 |
+ |
finally { |
444 |
+ |
lock.unlockRead(stamp); |
445 |
+ |
} |
446 |
+ |
stamp = lock.writeLock(); |
447 |
+ |
try { |
448 |
+ |
v = x; |
449 |
+ |
} finally { |
450 |
+ |
lock.unlockWrite(stamp); |
451 |
+ |
} |
452 |
+ |
sum += LoopHelpers.compute2(v); |
453 |
+ |
} |
454 |
+ |
return sum; |
455 |
+ |
} |
456 |
+ |
} |
457 |
|
} |