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.*; |
92 |
|
Thread.sleep(10); |
93 |
|
|
94 |
|
if (print) |
95 |
+ |
System.out.print("ReentrantReadLock "); |
96 |
+ |
new ReentrantReadLockLoop().test(v, nthreads, iters); |
97 |
+ |
Thread.sleep(10); |
98 |
+ |
|
99 |
+ |
if (print) |
100 |
|
System.out.print("ReentrantReadWriteLock"); |
101 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
102 |
|
Thread.sleep(10); |
276 |
|
final int loop(int n) { |
277 |
|
int sum = 0; |
278 |
|
while (n-- > 0) { |
279 |
+ |
lock.lock(); |
280 |
+ |
try { |
281 |
+ |
v = LoopHelpers.compute1(v); |
282 |
+ |
} |
283 |
+ |
finally { |
284 |
+ |
lock.unlock(); |
285 |
+ |
} |
286 |
+ |
sum += LoopHelpers.compute2(v); |
287 |
+ |
} |
288 |
+ |
return sum; |
289 |
+ |
} |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
private static class ReentrantReadLockLoop extends LockLoop { |
293 |
+ |
private final Lock lock = new ReentrantReadWriteLock().readLock(); |
294 |
+ |
final int loop(int n) { |
295 |
+ |
int sum = 0; |
296 |
+ |
while (n-- > 0) { |
297 |
|
lock.lock(); |
298 |
|
try { |
299 |
|
v = LoopHelpers.compute1(v); |