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 |
< |
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.locks.ReentrantLock; |
13 |
|
|
14 |
|
public final class CancelledLockLoops { |
15 |
|
static final Random rng = new Random(); |
16 |
|
static boolean print = false; |
17 |
|
static final int ITERS = 10000000; |
18 |
< |
static final long TIMEOUT = 100; |
18 |
> |
static final long TIMEOUT = 100; |
19 |
|
|
20 |
|
public static void main(String[] args) throws Exception { |
21 |
|
int maxThreads = 100; |
22 |
< |
if (args.length > 0) |
22 |
> |
if (args.length > 0) |
23 |
|
maxThreads = Integer.parseInt(args[0]); |
24 |
|
|
25 |
|
print = true; |
29 |
|
try { |
30 |
|
new ReentrantLockLoop(i).test(); |
31 |
|
} |
32 |
< |
catch(BrokenBarrierException bb) { |
32 |
> |
catch (BrokenBarrierException bb) { |
33 |
|
// OK, ignore |
34 |
|
} |
35 |
|
} |
50 |
|
|
51 |
|
final void test() throws Exception { |
52 |
|
Thread[] threads = new Thread[nthreads]; |
53 |
< |
for (int i = 0; i < threads.length; ++i) |
53 |
> |
for (int i = 0; i < threads.length; ++i) |
54 |
|
threads[i] = new Thread(this); |
55 |
< |
for (int i = 0; i < threads.length; ++i) |
55 |
> |
for (int i = 0; i < threads.length; ++i) |
56 |
|
threads[i].start(); |
57 |
< |
Thread[] cancels = (Thread[]) (threads.clone()); |
57 |
> |
Thread[] cancels = threads.clone(); |
58 |
|
Collections.shuffle(Arrays.asList(cancels), rng); |
59 |
|
barrier.await(); |
60 |
|
Thread.sleep(TIMEOUT); |
61 |
|
for (int i = 0; i < cancels.length-2; ++i) { |
62 |
|
cancels[i].interrupt(); |
63 |
|
// make sure all OK even when cancellations spaced out |
64 |
< |
if ( (i & 3) == 0) |
64 |
> |
if ( (i & 3) == 0) |
65 |
|
Thread.sleep(1 + rng.nextInt(10)); |
66 |
|
} |
67 |
|
barrier.await(); |
68 |
|
if (print) { |
69 |
|
long time = timer.getTime(); |
70 |
< |
double secs = (double)(time) / 1000000000.0; |
70 |
> |
double secs = (double) time / 1000000000.0; |
71 |
|
System.out.println("\t " + secs + "s run time"); |
72 |
|
} |
73 |
|
|
79 |
|
finally { |
80 |
|
lock.unlock(); |
81 |
|
} |
82 |
< |
if (completed != 2) |
82 |
> |
if (c != 2) |
83 |
|
throw new Error("Completed != 2"); |
84 |
|
int r = result; |
85 |
|
if (r == 0) // avoid overoptimization |
88 |
|
|
89 |
|
public final void run() { |
90 |
|
try { |
91 |
< |
barrier.await(); |
91 |
> |
barrier.await(); |
92 |
|
int sum = v; |
93 |
|
int x = 0; |
94 |
|
int n = ITERS; |
109 |
|
sum += LoopHelpers.compute2(x); |
110 |
|
} while (n-- > 0); |
111 |
|
if (n <= 0) { |
112 |
< |
lock.lock(); |
112 |
> |
lock.lock(); |
113 |
|
try { |
114 |
|
++completed; |
115 |
|
} |
120 |
|
barrier.await(); |
121 |
|
result += sum; |
122 |
|
} |
123 |
< |
catch (Exception ex) { |
123 |
> |
catch (Exception ex) { |
124 |
|
// ex.printStackTrace(); |
125 |
< |
return; |
125 |
> |
return; |
126 |
|
} |
127 |
|
} |
128 |
|
} |