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 |
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.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; |
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) |
28 |
> |
if (args.length > 0) |
29 |
|
maxThreads = Integer.parseInt(args[0]); |
30 |
|
|
31 |
|
print = true; |
52 |
|
} |
53 |
|
|
54 |
|
final void test() throws Exception { |
55 |
< |
for (int i = 0; i < nthreads; ++i) |
55 |
> |
for (int i = 0; i < nthreads; ++i) |
56 |
|
pool.execute(this); |
57 |
|
barrier.await(); |
58 |
|
Thread.sleep(TIMEOUT); |
60 |
|
barrier.await(); |
61 |
|
if (print) { |
62 |
|
long time = timer.getTime(); |
63 |
< |
double secs = (double)(time) / 1000000000.0; |
63 |
> |
double secs = (double) time / 1000000000.0; |
64 |
|
System.out.println("\t " + secs + "s run time"); |
65 |
|
} |
66 |
|
|
73 |
|
|
74 |
|
public final void run() { |
75 |
|
try { |
76 |
< |
barrier.await(); |
76 |
> |
barrier.await(); |
77 |
|
int sum = v; |
78 |
|
int x = 0; |
79 |
|
int n = ITERS; |
93 |
|
barrier.await(); |
94 |
|
result += sum; |
95 |
|
} |
96 |
< |
catch (Exception ie) { |
97 |
< |
return; |
96 |
> |
catch (Exception ie) { |
97 |
> |
return; |
98 |
|
} |
99 |
|
} |
100 |
|
} |