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