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; |
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 |
|
} |
142 |
|
|
143 |
< |
static abstract class LockLoop implements Runnable { |
143 |
> |
abstract static class LockLoop implements Runnable { |
144 |
|
int v; |
145 |
|
int iters; |
146 |
|
volatile int result; |
185 |
|
final int loop(int n) { |
186 |
|
int sum = 0; |
187 |
|
while (n-- > 0) { |
188 |
< |
synchronized(this) { |
188 |
> |
synchronized (this) { |
189 |
|
v = LoopHelpers.compute1(v); |
190 |
|
} |
191 |
|
sum += LoopHelpers.compute2(v); |
207 |
|
} |
208 |
|
|
209 |
|
private static class NoLockVolatileLoop extends LockLoop { |
210 |
< |
volatile private int vv; |
210 |
> |
private volatile int vv; |
211 |
|
final int loop(int n) { |
212 |
|
int sum = 0; |
213 |
|
while (n-- > 0) { |
220 |
|
} |
221 |
|
|
222 |
|
private static class ReentrantLockLoop extends LockLoop { |
223 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
223 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
224 |
|
final int loop(int n) { |
225 |
|
int sum = 0; |
226 |
|
while (n-- > 0) { |
238 |
|
} |
239 |
|
|
240 |
|
private static class FairReentrantLockLoop extends LockLoop { |
241 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
241 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
242 |
|
final int loop(int n) { |
243 |
|
int sum = 0; |
244 |
|
while (n-- > 0) { |
256 |
|
} |
257 |
|
|
258 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
259 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
259 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
260 |
|
final int loop(int n) { |
261 |
|
int sum = 0; |
262 |
|
while (n-- > 0) { |
274 |
|
} |
275 |
|
|
276 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
277 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
277 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
278 |
|
final int loop(int n) { |
279 |
|
int sum = 0; |
280 |
|
while (n-- > 0) { |
318 |
|
} |
319 |
|
|
320 |
|
private static class SemaphoreLoop extends LockLoop { |
321 |
< |
final private Semaphore sem = new Semaphore(1, false); |
321 |
> |
private final Semaphore sem = new Semaphore(1, false); |
322 |
|
final int loop(int n) { |
323 |
|
int sum = 0; |
324 |
|
try { |
339 |
|
return sum; |
340 |
|
} |
341 |
|
} |
342 |
+ |
|
343 |
|
private static class FairSemaphoreLoop extends LockLoop { |
344 |
< |
final private Semaphore sem = new Semaphore(1, true); |
344 |
> |
private final Semaphore sem = new Semaphore(1, true); |
345 |
|
final int loop(int n) { |
346 |
|
int sum = 0; |
347 |
|
try { |
364 |
|
} |
365 |
|
|
366 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
367 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
367 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
368 |
|
final int loop(int n) { |
369 |
|
int sum = 0; |
370 |
|
while (n-- > 0) { |
389 |
|
} |
390 |
|
} |
391 |
|
|
392 |
+ |
private static class StampedLockWriteLoop extends LockLoop { |
393 |
+ |
private final StampedLock lock = new StampedLock(); |
394 |
+ |
final int loop(int n) { |
395 |
+ |
int sum = 0; |
396 |
+ |
while (n-- > 0) { |
397 |
+ |
long stamp = lock.writeLock(); |
398 |
+ |
try { |
399 |
+ |
v = LoopHelpers.compute1(v); |
400 |
+ |
} |
401 |
+ |
finally { |
402 |
+ |
lock.unlockWrite(stamp); |
403 |
+ |
} |
404 |
+ |
sum += LoopHelpers.compute2(v); |
405 |
+ |
} |
406 |
+ |
return sum; |
407 |
+ |
} |
408 |
+ |
} |
409 |
|
|
410 |
+ |
private static class StampedLockReadLoop extends LockLoop { |
411 |
+ |
private final StampedLock lock = new StampedLock(); |
412 |
+ |
final int loop(int n) { |
413 |
+ |
int sum = 0; |
414 |
+ |
while (n-- > 0) { |
415 |
+ |
long stamp = lock.readLock(); |
416 |
+ |
try { |
417 |
+ |
v = LoopHelpers.compute1(v); |
418 |
+ |
} |
419 |
+ |
finally { |
420 |
+ |
lock.unlockRead(stamp); |
421 |
+ |
} |
422 |
+ |
sum += LoopHelpers.compute2(v); |
423 |
+ |
} |
424 |
+ |
return sum; |
425 |
+ |
} |
426 |
+ |
} |
427 |
|
} |