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("StampedLockOptRead"); |
143 |
+ |
new StampedLockOptimisticReadLoop().test(v, nthreads, iters); |
144 |
+ |
Thread.sleep(10); |
145 |
+ |
|
146 |
+ |
if (print) |
147 |
+ |
System.out.print("StampedLockReadWrite"); |
148 |
+ |
new StampedLockReadWriteLoop().test(v, nthreads, iters); |
149 |
+ |
Thread.sleep(10); |
150 |
+ |
} |
151 |
|
} |
152 |
|
|
153 |
< |
static abstract class LockLoop implements Runnable { |
153 |
> |
abstract static class LockLoop implements Runnable { |
154 |
|
int v; |
155 |
|
int iters; |
156 |
|
volatile int result; |
161 |
|
v = initialValue; |
162 |
|
this.iters = iters; |
163 |
|
barrier = new CyclicBarrier(nthreads+1, timer); |
164 |
< |
for (int i = 0; i < nthreads; ++i) |
164 |
> |
for (int i = 0; i < nthreads; ++i) |
165 |
|
pool.execute(this); |
166 |
|
barrier.await(); |
167 |
|
barrier.await(); |
169 |
|
if (print) { |
170 |
|
long tpi = time / (iters * nthreads); |
171 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
172 |
< |
// double secs = (double)(time) / 1000000000.0; |
172 |
> |
// double secs = (double) time / 1000000000.0; |
173 |
|
// System.out.print("\t " + secs + "s run time"); |
174 |
|
System.out.println(); |
175 |
|
} |
180 |
|
abstract int loop(int n); |
181 |
|
public final void run() { |
182 |
|
try { |
183 |
< |
barrier.await(); |
183 |
> |
barrier.await(); |
184 |
|
result += loop(iters); |
185 |
|
barrier.await(); |
186 |
|
} |
187 |
< |
catch (Exception ie) { |
188 |
< |
return; |
187 |
> |
catch (Exception ie) { |
188 |
> |
return; |
189 |
|
} |
190 |
|
} |
191 |
|
|
195 |
|
final int loop(int n) { |
196 |
|
int sum = 0; |
197 |
|
while (n-- > 0) { |
198 |
< |
synchronized(this) { |
198 |
> |
synchronized (this) { |
199 |
|
v = LoopHelpers.compute1(v); |
200 |
|
} |
201 |
|
sum += LoopHelpers.compute2(v); |
217 |
|
} |
218 |
|
|
219 |
|
private static class NoLockVolatileLoop extends LockLoop { |
220 |
< |
volatile private int vv; |
220 |
> |
private volatile int vv; |
221 |
|
final int loop(int n) { |
222 |
|
int sum = 0; |
223 |
|
while (n-- > 0) { |
230 |
|
} |
231 |
|
|
232 |
|
private static class ReentrantLockLoop extends LockLoop { |
233 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
233 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
234 |
|
final int loop(int n) { |
235 |
|
int sum = 0; |
236 |
|
while (n-- > 0) { |
248 |
|
} |
249 |
|
|
250 |
|
private static class FairReentrantLockLoop extends LockLoop { |
251 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
251 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
252 |
|
final int loop(int n) { |
253 |
|
int sum = 0; |
254 |
|
while (n-- > 0) { |
266 |
|
} |
267 |
|
|
268 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
269 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
269 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
270 |
|
final int loop(int n) { |
271 |
|
int sum = 0; |
272 |
|
while (n-- > 0) { |
284 |
|
} |
285 |
|
|
286 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
287 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
287 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
288 |
|
final int loop(int n) { |
289 |
|
int sum = 0; |
290 |
|
while (n-- > 0) { |
328 |
|
} |
329 |
|
|
330 |
|
private static class SemaphoreLoop extends LockLoop { |
331 |
< |
final private Semaphore sem = new Semaphore(1, false); |
331 |
> |
private final Semaphore sem = new Semaphore(1, false); |
332 |
|
final int loop(int n) { |
333 |
|
int sum = 0; |
334 |
|
try { |
349 |
|
return sum; |
350 |
|
} |
351 |
|
} |
352 |
+ |
|
353 |
|
private static class FairSemaphoreLoop extends LockLoop { |
354 |
< |
final private Semaphore sem = new Semaphore(1, true); |
354 |
> |
private final Semaphore sem = new Semaphore(1, true); |
355 |
|
final int loop(int n) { |
356 |
|
int sum = 0; |
357 |
|
try { |
374 |
|
} |
375 |
|
|
376 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
377 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
377 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
378 |
|
final int loop(int n) { |
379 |
|
int sum = 0; |
380 |
|
while (n-- > 0) { |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
+ |
private static class StampedLockWriteLoop extends LockLoop { |
403 |
+ |
private final StampedLock lock = new StampedLock(); |
404 |
+ |
final int loop(int n) { |
405 |
+ |
int sum = 0; |
406 |
+ |
while (n-- > 0) { |
407 |
+ |
long stamp = lock.writeLock(); |
408 |
+ |
try { |
409 |
+ |
v = LoopHelpers.compute1(v); |
410 |
+ |
} |
411 |
+ |
finally { |
412 |
+ |
lock.unlockWrite(stamp); |
413 |
+ |
} |
414 |
+ |
sum += LoopHelpers.compute2(v); |
415 |
+ |
} |
416 |
+ |
return sum; |
417 |
+ |
} |
418 |
+ |
} |
419 |
|
|
420 |
+ |
private static class StampedLockReadLoop extends LockLoop { |
421 |
+ |
private final StampedLock lock = new StampedLock(); |
422 |
+ |
final int loop(int n) { |
423 |
+ |
int sum = 0; |
424 |
+ |
while (n-- > 0) { |
425 |
+ |
long stamp = lock.readLock(); |
426 |
+ |
try { |
427 |
+ |
v = LoopHelpers.compute1(v); |
428 |
+ |
} |
429 |
+ |
finally { |
430 |
+ |
lock.unlockRead(stamp); |
431 |
+ |
} |
432 |
+ |
sum += LoopHelpers.compute2(v); |
433 |
+ |
} |
434 |
+ |
return sum; |
435 |
+ |
} |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
private static class StampedLockOptimisticReadLoop extends LockLoop { |
439 |
+ |
private final StampedLock lock = new StampedLock(); |
440 |
+ |
final int loop(int n) { |
441 |
+ |
int sum = 0; |
442 |
+ |
while (n-- > 0) { |
443 |
+ |
long stamp; |
444 |
+ |
do { |
445 |
+ |
stamp = lock.tryOptimisticRead(); |
446 |
+ |
v = LoopHelpers.compute1(v); |
447 |
+ |
} while (!lock.validate(stamp)); |
448 |
+ |
sum += LoopHelpers.compute2(v); |
449 |
+ |
} |
450 |
+ |
return sum; |
451 |
+ |
} |
452 |
+ |
} |
453 |
+ |
|
454 |
+ |
private static class StampedLockReadWriteLoop extends LockLoop { |
455 |
+ |
private final StampedLock lock = new StampedLock(); |
456 |
+ |
final int loop(int n) { |
457 |
+ |
int sum = 0; |
458 |
+ |
while (n-- > 0) { |
459 |
+ |
int x; |
460 |
+ |
long stamp = lock.readLock(); |
461 |
+ |
try { |
462 |
+ |
x = LoopHelpers.compute1(v); |
463 |
+ |
} |
464 |
+ |
finally { |
465 |
+ |
lock.unlockRead(stamp); |
466 |
+ |
} |
467 |
+ |
stamp = lock.writeLock(); |
468 |
+ |
try { |
469 |
+ |
v = x; |
470 |
+ |
} finally { |
471 |
+ |
lock.unlockWrite(stamp); |
472 |
+ |
} |
473 |
+ |
sum += LoopHelpers.compute2(v); |
474 |
+ |
} |
475 |
+ |
return sum; |
476 |
+ |
} |
477 |
+ |
} |
478 |
|
} |