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 |
|
* @test |
11 |
|
* decade. |
12 |
|
*/ |
13 |
|
|
14 |
< |
import java.util.concurrent.*; |
15 |
< |
import java.util.concurrent.locks.*; |
16 |
< |
import java.util.*; |
14 |
> |
import java.util.concurrent.CyclicBarrier; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.ExecutorService; |
17 |
> |
import java.util.concurrent.Executors; |
18 |
> |
import java.util.concurrent.TimeUnit; |
19 |
|
|
20 |
|
public final class TimeoutMutexLoops { |
21 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
22 |
|
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
23 |
|
static boolean print = false; |
24 |
|
static final int ITERS = Integer.MAX_VALUE; |
25 |
< |
static final long TIMEOUT = 100; |
25 |
> |
static final long TIMEOUT = 100; |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
28 |
|
int maxThreads = 100; |
29 |
< |
if (args.length > 0) |
29 |
> |
if (args.length > 0) |
30 |
|
maxThreads = Integer.parseInt(args[0]); |
31 |
|
|
32 |
|
print = true; |
53 |
|
} |
54 |
|
|
55 |
|
final void test() throws Exception { |
56 |
< |
for (int i = 0; i < nthreads; ++i) |
56 |
> |
for (int i = 0; i < nthreads; ++i) |
57 |
|
pool.execute(this); |
58 |
|
barrier.await(); |
59 |
|
Thread.sleep(TIMEOUT); |
61 |
|
barrier.await(); |
62 |
|
if (print) { |
63 |
|
long time = timer.getTime(); |
64 |
< |
double secs = (double)(time) / 1000000000.0; |
64 |
> |
double secs = (double) time / 1000000000.0; |
65 |
|
System.out.println("\t " + secs + "s run time"); |
66 |
|
} |
67 |
|
|
74 |
|
|
75 |
|
public final void run() { |
76 |
|
try { |
77 |
< |
barrier.await(); |
77 |
> |
barrier.await(); |
78 |
|
int sum = v; |
79 |
|
int x = 0; |
80 |
|
int n = ITERS; |
94 |
|
barrier.await(); |
95 |
|
result += sum; |
96 |
|
} |
97 |
< |
catch (Exception ie) { |
98 |
< |
return; |
97 |
> |
catch (Exception ie) { |
98 |
> |
return; |
99 |
|
} |
100 |
|
} |
101 |
|
} |