12 |
|
static final Random rng = new Random(); |
13 |
|
static boolean print = false; |
14 |
|
static final int ITERS = 10000000; |
15 |
< |
static final long TIMEOUT = 100; |
15 |
> |
static final long TIMEOUT = 100; |
16 |
|
|
17 |
|
public static void main(String[] args) throws Exception { |
18 |
|
int maxThreads = 100; |
19 |
< |
if (args.length > 0) |
19 |
> |
if (args.length > 0) |
20 |
|
maxThreads = Integer.parseInt(args[0]); |
21 |
|
|
22 |
|
print = true; |
26 |
|
try { |
27 |
|
new ReentrantLockLoop(i).test(); |
28 |
|
} |
29 |
< |
catch(BrokenBarrierException bb) { |
29 |
> |
catch (BrokenBarrierException bb) { |
30 |
|
// OK, ignore |
31 |
|
} |
32 |
|
} |
47 |
|
|
48 |
|
final void test() throws Exception { |
49 |
|
Thread[] threads = new Thread[nthreads]; |
50 |
< |
for (int i = 0; i < threads.length; ++i) |
50 |
> |
for (int i = 0; i < threads.length; ++i) |
51 |
|
threads[i] = new Thread(this); |
52 |
< |
for (int i = 0; i < threads.length; ++i) |
52 |
> |
for (int i = 0; i < threads.length; ++i) |
53 |
|
threads[i].start(); |
54 |
< |
Thread[] cancels = (Thread[]) (threads.clone()); |
54 |
> |
Thread[] cancels = threads.clone(); |
55 |
|
Collections.shuffle(Arrays.asList(cancels), rng); |
56 |
|
barrier.await(); |
57 |
|
Thread.sleep(TIMEOUT); |
58 |
|
for (int i = 0; i < cancels.length-2; ++i) { |
59 |
|
cancels[i].interrupt(); |
60 |
|
// make sure all OK even when cancellations spaced out |
61 |
< |
if ( (i & 3) == 0) |
61 |
> |
if ( (i & 3) == 0) |
62 |
|
Thread.sleep(1 + rng.nextInt(10)); |
63 |
|
} |
64 |
|
barrier.await(); |
65 |
|
if (print) { |
66 |
|
long time = timer.getTime(); |
67 |
< |
double secs = (double)(time) / 1000000000.0; |
67 |
> |
double secs = (double) time / 1000000000.0; |
68 |
|
System.out.println("\t " + secs + "s run time"); |
69 |
|
} |
70 |
|
|
76 |
|
finally { |
77 |
|
lock.unlock(); |
78 |
|
} |
79 |
< |
if (completed != 2) |
79 |
> |
if (c != 2) |
80 |
|
throw new Error("Completed != 2"); |
81 |
|
int r = result; |
82 |
|
if (r == 0) // avoid overoptimization |
85 |
|
|
86 |
|
public final void run() { |
87 |
|
try { |
88 |
< |
barrier.await(); |
88 |
> |
barrier.await(); |
89 |
|
int sum = v; |
90 |
|
int x = 0; |
91 |
|
int n = ITERS; |
106 |
|
sum += LoopHelpers.compute2(x); |
107 |
|
} while (n-- > 0); |
108 |
|
if (n <= 0) { |
109 |
< |
lock.lock(); |
109 |
> |
lock.lock(); |
110 |
|
try { |
111 |
|
++completed; |
112 |
|
} |
117 |
|
barrier.await(); |
118 |
|
result += sum; |
119 |
|
} |
120 |
< |
catch (Exception ex) { |
120 |
> |
catch (Exception ex) { |
121 |
|
// ex.printStackTrace(); |
122 |
< |
return; |
122 |
> |
return; |
123 |
|
} |
124 |
|
} |
125 |
|
} |