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 |
|
/* |
8 |
< |
* A simple test program. Feel free to play. |
8 |
> |
* Simple benchmark comparing various locking techniques. |
9 |
|
*/ |
10 |
|
|
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.util.concurrent.locks.*; |
13 |
< |
import java.util.*; |
11 |
> |
import java.util.concurrent.CyclicBarrier; |
12 |
> |
import java.util.concurrent.ExecutorService; |
13 |
> |
import java.util.concurrent.Executors; |
14 |
> |
import java.util.concurrent.Semaphore; |
15 |
> |
import java.util.concurrent.locks.Lock; |
16 |
> |
import java.util.concurrent.locks.ReentrantLock; |
17 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
18 |
> |
import java.util.concurrent.locks.StampedLock; |
19 |
|
|
20 |
|
public final class LockLoops { |
21 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
24 |
|
static boolean doBuiltin = true; |
25 |
|
static boolean doReadWrite = true; |
26 |
|
static boolean doSemaphore = true; |
27 |
< |
static boolean doFair = true; |
27 |
> |
static boolean doStampedLock = true; |
28 |
> |
static boolean doFair = true; |
29 |
|
|
30 |
|
public static void main(String[] args) throws Exception { |
31 |
|
int maxThreads = 100; |
97 |
|
Thread.sleep(10); |
98 |
|
|
99 |
|
if (print) |
100 |
+ |
System.out.print("ReentrantReadLock "); |
101 |
+ |
new ReentrantReadLockLoop().test(v, nthreads, iters); |
102 |
+ |
Thread.sleep(10); |
103 |
+ |
|
104 |
+ |
if (print) |
105 |
|
System.out.print("ReentrantReadWriteLock"); |
106 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
107 |
|
Thread.sleep(10); |
96 |
– |
|
108 |
|
} |
109 |
|
|
110 |
|
if (doSemaphore) { |
117 |
|
System.out.print("FairSemaphore "); |
118 |
|
new FairSemaphoreLoop().test(v, nthreads, iters); |
119 |
|
Thread.sleep(10); |
109 |
– |
|
120 |
|
} |
121 |
|
|
122 |
|
if (doFair) { |
136 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); |
137 |
|
Thread.sleep(10); |
138 |
|
} |
129 |
– |
|
139 |
|
} |
140 |
|
|
141 |
+ |
if (doStampedLock) { |
142 |
+ |
if (print) |
143 |
+ |
System.out.print("StampedLockWrite "); |
144 |
+ |
new StampedLockWriteLoop().test(v, nthreads, iters); |
145 |
+ |
Thread.sleep(10); |
146 |
+ |
|
147 |
+ |
if (print) |
148 |
+ |
System.out.print("StampedLockRead "); |
149 |
+ |
new StampedLockReadLoop().test(v, nthreads, iters); |
150 |
+ |
Thread.sleep(10); |
151 |
+ |
|
152 |
+ |
if (print) |
153 |
+ |
System.out.print("StampedLockOptRead"); |
154 |
+ |
new StampedLockOptimisticReadLoop().test(v, nthreads, iters); |
155 |
+ |
Thread.sleep(10); |
156 |
+ |
|
157 |
+ |
if (print) |
158 |
+ |
System.out.print("StampedLockReadWrite"); |
159 |
+ |
new StampedLockReadWriteLoop().test(v, nthreads, iters); |
160 |
+ |
Thread.sleep(10); |
161 |
+ |
} |
162 |
|
} |
163 |
|
|
164 |
< |
static abstract class LockLoop implements Runnable { |
164 |
> |
abstract static class LockLoop implements Runnable { |
165 |
|
int v; |
166 |
|
int iters; |
167 |
|
volatile int result; |
228 |
|
} |
229 |
|
|
230 |
|
private static class NoLockVolatileLoop extends LockLoop { |
231 |
< |
volatile private int vv; |
231 |
> |
private volatile int vv; |
232 |
|
final int loop(int n) { |
233 |
|
int sum = 0; |
234 |
|
while (n-- > 0) { |
241 |
|
} |
242 |
|
|
243 |
|
private static class ReentrantLockLoop extends LockLoop { |
244 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
244 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
245 |
|
final int loop(int n) { |
246 |
|
int sum = 0; |
247 |
|
while (n-- > 0) { |
259 |
|
} |
260 |
|
|
261 |
|
private static class FairReentrantLockLoop extends LockLoop { |
262 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
262 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
263 |
|
final int loop(int n) { |
264 |
|
int sum = 0; |
265 |
|
while (n-- > 0) { |
277 |
|
} |
278 |
|
|
279 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
280 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
280 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
281 |
> |
final int loop(int n) { |
282 |
> |
int sum = 0; |
283 |
> |
while (n-- > 0) { |
284 |
> |
lock.lock(); |
285 |
> |
try { |
286 |
> |
v = LoopHelpers.compute1(v); |
287 |
> |
} |
288 |
> |
finally { |
289 |
> |
lock.unlock(); |
290 |
> |
} |
291 |
> |
sum += LoopHelpers.compute2(v); |
292 |
> |
} |
293 |
> |
return sum; |
294 |
> |
} |
295 |
> |
} |
296 |
> |
|
297 |
> |
private static class ReentrantReadLockLoop extends LockLoop { |
298 |
> |
private final Lock lock = new ReentrantReadWriteLock().readLock(); |
299 |
|
final int loop(int n) { |
300 |
|
int sum = 0; |
301 |
|
while (n-- > 0) { |
313 |
|
} |
314 |
|
|
315 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
316 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
316 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
317 |
|
final int loop(int n) { |
318 |
|
int sum = 0; |
319 |
|
while (n-- > 0) { |
357 |
|
} |
358 |
|
|
359 |
|
private static class SemaphoreLoop extends LockLoop { |
360 |
< |
final private Semaphore sem = new Semaphore(1, false); |
360 |
> |
private final Semaphore sem = new Semaphore(1, false); |
361 |
|
final int loop(int n) { |
362 |
|
int sum = 0; |
363 |
|
try { |
378 |
|
return sum; |
379 |
|
} |
380 |
|
} |
381 |
+ |
|
382 |
|
private static class FairSemaphoreLoop extends LockLoop { |
383 |
< |
final private Semaphore sem = new Semaphore(1, true); |
383 |
> |
private final Semaphore sem = new Semaphore(1, true); |
384 |
|
final int loop(int n) { |
385 |
|
int sum = 0; |
386 |
|
try { |
403 |
|
} |
404 |
|
|
405 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
406 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
406 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
407 |
|
final int loop(int n) { |
408 |
|
int sum = 0; |
409 |
|
while (n-- > 0) { |
428 |
|
} |
429 |
|
} |
430 |
|
|
431 |
+ |
private static class StampedLockWriteLoop extends LockLoop { |
432 |
+ |
private final StampedLock lock = new StampedLock(); |
433 |
+ |
final int loop(int n) { |
434 |
+ |
int sum = 0; |
435 |
+ |
while (n-- > 0) { |
436 |
+ |
long stamp = lock.writeLock(); |
437 |
+ |
try { |
438 |
+ |
v = LoopHelpers.compute1(v); |
439 |
+ |
} |
440 |
+ |
finally { |
441 |
+ |
lock.unlockWrite(stamp); |
442 |
+ |
} |
443 |
+ |
sum += LoopHelpers.compute2(v); |
444 |
+ |
} |
445 |
+ |
return sum; |
446 |
+ |
} |
447 |
+ |
} |
448 |
+ |
|
449 |
+ |
private static class StampedLockReadLoop extends LockLoop { |
450 |
+ |
private final StampedLock lock = new StampedLock(); |
451 |
+ |
final int loop(int n) { |
452 |
+ |
int sum = 0; |
453 |
+ |
while (n-- > 0) { |
454 |
+ |
long stamp = lock.readLock(); |
455 |
+ |
try { |
456 |
+ |
v = LoopHelpers.compute1(v); |
457 |
+ |
} |
458 |
+ |
finally { |
459 |
+ |
lock.unlockRead(stamp); |
460 |
+ |
} |
461 |
+ |
sum += LoopHelpers.compute2(v); |
462 |
+ |
} |
463 |
+ |
return sum; |
464 |
+ |
} |
465 |
+ |
} |
466 |
|
|
467 |
+ |
private static class StampedLockOptimisticReadLoop extends LockLoop { |
468 |
+ |
private final StampedLock lock = new StampedLock(); |
469 |
+ |
final int loop(int n) { |
470 |
+ |
int sum = 0; |
471 |
+ |
while (n-- > 0) { |
472 |
+ |
long stamp; |
473 |
+ |
do { |
474 |
+ |
stamp = lock.tryOptimisticRead(); |
475 |
+ |
v = LoopHelpers.compute1(v); |
476 |
+ |
} while (!lock.validate(stamp)); |
477 |
+ |
sum += LoopHelpers.compute2(v); |
478 |
+ |
} |
479 |
+ |
return sum; |
480 |
+ |
} |
481 |
+ |
} |
482 |
+ |
|
483 |
+ |
private static class StampedLockReadWriteLoop extends LockLoop { |
484 |
+ |
private final StampedLock lock = new StampedLock(); |
485 |
+ |
final int loop(int n) { |
486 |
+ |
int sum = 0; |
487 |
+ |
while (n-- > 0) { |
488 |
+ |
int x; |
489 |
+ |
long stamp = lock.readLock(); |
490 |
+ |
try { |
491 |
+ |
x = LoopHelpers.compute1(v); |
492 |
+ |
} |
493 |
+ |
finally { |
494 |
+ |
lock.unlockRead(stamp); |
495 |
+ |
} |
496 |
+ |
stamp = lock.writeLock(); |
497 |
+ |
try { |
498 |
+ |
v = x; |
499 |
+ |
} finally { |
500 |
+ |
lock.unlockWrite(stamp); |
501 |
+ |
} |
502 |
+ |
sum += LoopHelpers.compute2(v); |
503 |
+ |
} |
504 |
+ |
return sum; |
505 |
+ |
} |
506 |
+ |
} |
507 |
|
} |