3 |
|
* Expert Group and released to the public domain, as explained at |
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.*; |
12 |
< |
import java.util.concurrent.*; |
13 |
< |
import java.util.concurrent.locks.*; |
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(); |
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); |
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 StampedLockReadLoop().test(v, nthreads, iters); |
159 |
> |
new StampedLockReadWriteLoop().test(v, nthreads, iters); |
160 |
|
Thread.sleep(10); |
161 |
|
} |
162 |
|
} |
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) { |
302 |
+ |
lock.lock(); |
303 |
+ |
try { |
304 |
+ |
v = LoopHelpers.compute1(v); |
305 |
+ |
} |
306 |
+ |
finally { |
307 |
+ |
lock.unlock(); |
308 |
+ |
} |
309 |
+ |
sum += LoopHelpers.compute2(v); |
310 |
+ |
} |
311 |
+ |
return sum; |
312 |
+ |
} |
313 |
+ |
} |
314 |
+ |
|
315 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
316 |
|
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
317 |
|
final int loop(int n) { |
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 |
|
|