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. |
18 |
|
static boolean doBuiltin = true; |
19 |
|
static boolean doReadWrite = true; |
20 |
|
static boolean doSemaphore = true; |
21 |
< |
static boolean doFair = true; |
21 |
> |
static boolean doFair = true; |
22 |
|
|
23 |
|
public static void main(String[] args) throws Exception { |
24 |
|
int maxThreads = 100; |
131 |
|
|
132 |
|
} |
133 |
|
|
134 |
< |
static abstract class LockLoop implements Runnable { |
134 |
> |
abstract static class LockLoop implements Runnable { |
135 |
|
int v; |
136 |
|
int iters; |
137 |
|
volatile int result; |
198 |
|
} |
199 |
|
|
200 |
|
private static class NoLockVolatileLoop extends LockLoop { |
201 |
< |
volatile private int vv; |
201 |
> |
private volatile int vv; |
202 |
|
final int loop(int n) { |
203 |
|
int sum = 0; |
204 |
|
while (n-- > 0) { |
211 |
|
} |
212 |
|
|
213 |
|
private static class ReentrantLockLoop extends LockLoop { |
214 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
214 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
215 |
|
final int loop(int n) { |
216 |
|
int sum = 0; |
217 |
|
while (n-- > 0) { |
229 |
|
} |
230 |
|
|
231 |
|
private static class FairReentrantLockLoop extends LockLoop { |
232 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
232 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
233 |
|
final int loop(int n) { |
234 |
|
int sum = 0; |
235 |
|
while (n-- > 0) { |
247 |
|
} |
248 |
|
|
249 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
250 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
250 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
251 |
|
final int loop(int n) { |
252 |
|
int sum = 0; |
253 |
|
while (n-- > 0) { |
265 |
|
} |
266 |
|
|
267 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
268 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
268 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
269 |
|
final int loop(int n) { |
270 |
|
int sum = 0; |
271 |
|
while (n-- > 0) { |
309 |
|
} |
310 |
|
|
311 |
|
private static class SemaphoreLoop extends LockLoop { |
312 |
< |
final private Semaphore sem = new Semaphore(1, false); |
312 |
> |
private final Semaphore sem = new Semaphore(1, false); |
313 |
|
final int loop(int n) { |
314 |
|
int sum = 0; |
315 |
|
try { |
331 |
|
} |
332 |
|
} |
333 |
|
private static class FairSemaphoreLoop extends LockLoop { |
334 |
< |
final private Semaphore sem = new Semaphore(1, true); |
334 |
> |
private final Semaphore sem = new Semaphore(1, true); |
335 |
|
final int loop(int n) { |
336 |
|
int sum = 0; |
337 |
|
try { |
354 |
|
} |
355 |
|
|
356 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
357 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
357 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
358 |
|
final int loop(int n) { |
359 |
|
int sum = 0; |
360 |
|
while (n-- > 0) { |