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 doFair = true; |
22 |
|
|
23 |
|
public static void main(String[] args) throws Exception { |
24 |
|
int maxThreads = 100; |
93 |
|
System.out.print("ReentrantReadWriteLock"); |
94 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
95 |
|
Thread.sleep(10); |
96 |
– |
|
96 |
|
} |
97 |
|
|
98 |
|
if (doSemaphore) { |
105 |
|
System.out.print("FairSemaphore "); |
106 |
|
new FairSemaphoreLoop().test(v, nthreads, iters); |
107 |
|
Thread.sleep(10); |
109 |
– |
|
108 |
|
} |
109 |
|
|
110 |
|
if (doFair) { |
124 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); |
125 |
|
Thread.sleep(10); |
126 |
|
} |
129 |
– |
|
127 |
|
} |
131 |
– |
|
128 |
|
} |
129 |
|
|
130 |
< |
static abstract class LockLoop implements Runnable { |
130 |
> |
abstract static class LockLoop implements Runnable { |
131 |
|
int v; |
132 |
|
int iters; |
133 |
|
volatile int result; |
146 |
|
if (print) { |
147 |
|
long tpi = time / (iters * nthreads); |
148 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
149 |
< |
// double secs = (double)(time) / 1000000000.0; |
149 |
> |
// double secs = (double) time / 1000000000.0; |
150 |
|
// System.out.print("\t " + secs + "s run time"); |
151 |
|
System.out.println(); |
152 |
|
} |
172 |
|
final int loop(int n) { |
173 |
|
int sum = 0; |
174 |
|
while (n-- > 0) { |
175 |
< |
synchronized(this) { |
175 |
> |
synchronized (this) { |
176 |
|
v = LoopHelpers.compute1(v); |
177 |
|
} |
178 |
|
sum += LoopHelpers.compute2(v); |
194 |
|
} |
195 |
|
|
196 |
|
private static class NoLockVolatileLoop extends LockLoop { |
197 |
< |
volatile private int vv; |
197 |
> |
private volatile int vv; |
198 |
|
final int loop(int n) { |
199 |
|
int sum = 0; |
200 |
|
while (n-- > 0) { |
207 |
|
} |
208 |
|
|
209 |
|
private static class ReentrantLockLoop extends LockLoop { |
210 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
210 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
211 |
|
final int loop(int n) { |
212 |
|
int sum = 0; |
213 |
|
while (n-- > 0) { |
225 |
|
} |
226 |
|
|
227 |
|
private static class FairReentrantLockLoop extends LockLoop { |
228 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
228 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
229 |
|
final int loop(int n) { |
230 |
|
int sum = 0; |
231 |
|
while (n-- > 0) { |
243 |
|
} |
244 |
|
|
245 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
246 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
246 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
247 |
|
final int loop(int n) { |
248 |
|
int sum = 0; |
249 |
|
while (n-- > 0) { |
261 |
|
} |
262 |
|
|
263 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
264 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
264 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
265 |
|
final int loop(int n) { |
266 |
|
int sum = 0; |
267 |
|
while (n-- > 0) { |
305 |
|
} |
306 |
|
|
307 |
|
private static class SemaphoreLoop extends LockLoop { |
308 |
< |
final private Semaphore sem = new Semaphore(1, false); |
308 |
> |
private final Semaphore sem = new Semaphore(1, false); |
309 |
|
final int loop(int n) { |
310 |
|
int sum = 0; |
311 |
|
try { |
327 |
|
} |
328 |
|
} |
329 |
|
private static class FairSemaphoreLoop extends LockLoop { |
330 |
< |
final private Semaphore sem = new Semaphore(1, true); |
330 |
> |
private final Semaphore sem = new Semaphore(1, true); |
331 |
|
final int loop(int n) { |
332 |
|
int sum = 0; |
333 |
|
try { |
350 |
|
} |
351 |
|
|
352 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
353 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
353 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
354 |
|
final int loop(int n) { |
355 |
|
int sum = 0; |
356 |
|
while (n-- > 0) { |
374 |
|
return sum; |
375 |
|
} |
376 |
|
} |
381 |
– |
|
382 |
– |
|
377 |
|
} |