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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
/* |
7 |
* @test |
8 |
* Checks for responsiveness of locks to timeouts. Runs under the |
9 |
* assumption that ITERS computations require more than TIMEOUT msecs |
10 |
* to complete, which seems to be a safe assumption for another |
11 |
* decade. |
12 |
*/ |
13 |
|
14 |
import java.util.*; |
15 |
import java.util.concurrent.*; |
16 |
import java.util.concurrent.locks.*; |
17 |
|
18 |
public final class TimeoutMutexLoops { |
19 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
20 |
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
21 |
static boolean print = false; |
22 |
static final int ITERS = Integer.MAX_VALUE; |
23 |
static final long TIMEOUT = 100; |
24 |
|
25 |
public static void main(String[] args) throws Exception { |
26 |
int maxThreads = 100; |
27 |
if (args.length > 0) |
28 |
maxThreads = Integer.parseInt(args[0]); |
29 |
|
30 |
print = true; |
31 |
|
32 |
for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) { |
33 |
System.out.print("Threads: " + i); |
34 |
new MutexLoop(i).test(); |
35 |
Thread.sleep(TIMEOUT); |
36 |
} |
37 |
pool.shutdown(); |
38 |
} |
39 |
|
40 |
static final class MutexLoop implements Runnable { |
41 |
private int v = rng.next(); |
42 |
private volatile boolean completed; |
43 |
private volatile int result = 17; |
44 |
private final Mutex lock = new Mutex(); |
45 |
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
46 |
private final CyclicBarrier barrier; |
47 |
private final int nthreads; |
48 |
MutexLoop(int nthreads) { |
49 |
this.nthreads = nthreads; |
50 |
barrier = new CyclicBarrier(nthreads+1, timer); |
51 |
} |
52 |
|
53 |
final void test() throws Exception { |
54 |
for (int i = 0; i < nthreads; ++i) |
55 |
pool.execute(this); |
56 |
barrier.await(); |
57 |
Thread.sleep(TIMEOUT); |
58 |
lock.lock(); |
59 |
barrier.await(); |
60 |
if (print) { |
61 |
long time = timer.getTime(); |
62 |
double secs = (double) time / 1000000000.0; |
63 |
System.out.println("\t " + secs + "s run time"); |
64 |
} |
65 |
|
66 |
if (completed) |
67 |
throw new Error("Some thread completed instead of timing out"); |
68 |
int r = result; |
69 |
if (r == 0) // avoid overoptimization |
70 |
System.out.println("useless result: " + r); |
71 |
} |
72 |
|
73 |
public final void run() { |
74 |
try { |
75 |
barrier.await(); |
76 |
int sum = v; |
77 |
int x = 0; |
78 |
int n = ITERS; |
79 |
do { |
80 |
if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) |
81 |
break; |
82 |
try { |
83 |
v = x = LoopHelpers.compute1(v); |
84 |
} |
85 |
finally { |
86 |
lock.unlock(); |
87 |
} |
88 |
sum += LoopHelpers.compute2(x); |
89 |
} while (n-- > 0); |
90 |
if (n <= 0) |
91 |
completed = true; |
92 |
barrier.await(); |
93 |
result += sum; |
94 |
} |
95 |
catch (Exception ie) { |
96 |
return; |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
} |