1 |
|
/* |
2 |
– |
* @test |
3 |
– |
* @summary tests lockInterruptibly |
4 |
– |
* Checks for responsiveness of locks to interrupts. Runs under that |
5 |
– |
* assumption that ITERS_VALUE computations require more than TIMEOUT |
6 |
– |
* msecs to complete. |
7 |
– |
*/ |
8 |
– |
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import java.util.concurrent.*; |
8 |
< |
import java.util.concurrent.locks.*; |
9 |
< |
import java.util.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.Collections; |
9 |
> |
import java.util.Random; |
10 |
> |
import java.util.concurrent.BrokenBarrierException; |
11 |
> |
import java.util.concurrent.CyclicBarrier; |
12 |
> |
import java.util.concurrent.ExecutorService; |
13 |
> |
import java.util.concurrent.Executors; |
14 |
> |
import java.util.concurrent.locks.ReentrantLock; |
15 |
|
|
16 |
|
public final class CancelledLockLoops { |
17 |
|
static final Random rng = new Random(); |
18 |
|
static boolean print = false; |
19 |
|
static final int ITERS = 10000000; |
20 |
< |
static final long TIMEOUT = 100; |
20 |
> |
static final long TIMEOUT = 100; |
21 |
|
|
22 |
|
public static void main(String[] args) throws Exception { |
23 |
|
int maxThreads = 100; |
24 |
< |
if (args.length > 0) |
24 |
> |
if (args.length > 0) |
25 |
|
maxThreads = Integer.parseInt(args[0]); |
26 |
|
|
27 |
|
print = true; |
31 |
|
try { |
32 |
|
new ReentrantLockLoop(i).test(); |
33 |
|
} |
34 |
< |
catch(BrokenBarrierException bb) { |
34 |
> |
catch (BrokenBarrierException bb) { |
35 |
|
// OK, ignore |
36 |
|
} |
37 |
|
} |
52 |
|
|
53 |
|
final void test() throws Exception { |
54 |
|
Thread[] threads = new Thread[nthreads]; |
55 |
< |
for (int i = 0; i < threads.length; ++i) |
55 |
> |
for (int i = 0; i < threads.length; ++i) |
56 |
|
threads[i] = new Thread(this); |
57 |
< |
for (int i = 0; i < threads.length; ++i) |
57 |
> |
for (int i = 0; i < threads.length; ++i) |
58 |
|
threads[i].start(); |
59 |
< |
Thread[] cancels = (Thread[]) (threads.clone()); |
59 |
> |
Thread[] cancels = threads.clone(); |
60 |
|
Collections.shuffle(Arrays.asList(cancels), rng); |
61 |
|
barrier.await(); |
62 |
|
Thread.sleep(TIMEOUT); |
63 |
|
for (int i = 0; i < cancels.length-2; ++i) { |
64 |
|
cancels[i].interrupt(); |
65 |
|
// make sure all OK even when cancellations spaced out |
66 |
< |
if ( (i & 3) == 0) |
66 |
> |
if ( (i & 3) == 0) |
67 |
|
Thread.sleep(1 + rng.nextInt(10)); |
68 |
|
} |
69 |
|
barrier.await(); |
70 |
|
if (print) { |
71 |
|
long time = timer.getTime(); |
72 |
< |
double secs = (double)(time) / 1000000000.0; |
72 |
> |
double secs = (double) time / 1000000000.0; |
73 |
|
System.out.println("\t " + secs + "s run time"); |
74 |
|
} |
75 |
|
|
81 |
|
finally { |
82 |
|
lock.unlock(); |
83 |
|
} |
84 |
< |
if (completed != 2) |
84 |
> |
if (c != 2) |
85 |
|
throw new Error("Completed != 2"); |
86 |
|
int r = result; |
87 |
|
if (r == 0) // avoid overoptimization |
90 |
|
|
91 |
|
public final void run() { |
92 |
|
try { |
93 |
< |
barrier.await(); |
93 |
> |
barrier.await(); |
94 |
|
int sum = v; |
95 |
|
int x = 0; |
96 |
|
int n = ITERS; |
111 |
|
sum += LoopHelpers.compute2(x); |
112 |
|
} while (n-- > 0); |
113 |
|
if (n <= 0) { |
114 |
< |
lock.lock(); |
114 |
> |
lock.lock(); |
115 |
|
try { |
116 |
|
++completed; |
117 |
|
} |
122 |
|
barrier.await(); |
123 |
|
result += sum; |
124 |
|
} |
125 |
< |
catch (Exception ex) { |
125 |
> |
catch (Exception ex) { |
126 |
|
// ex.printStackTrace(); |
127 |
< |
return; |
127 |
> |
return; |
128 |
|
} |
129 |
|
} |
130 |
|
} |