ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutMutexLoops.java
Revision: 1.7
Committed: Sat Dec 31 19:25:33 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +5 -3 lines
Log Message:
organize imports

File Contents

# Content
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
10 * to complete, which seems to be a safe assumption for another
11 * decade.
12 */
13
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;
26
27 public static void main(String[] args) throws Exception {
28 int maxThreads = 100;
29 if (args.length > 0)
30 maxThreads = Integer.parseInt(args[0]);
31
32 print = true;
33
34 for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
35 System.out.print("Threads: " + i);
36 new MutexLoop(i).test();
37 Thread.sleep(TIMEOUT);
38 }
39 pool.shutdown();
40 }
41
42 static final class MutexLoop implements Runnable {
43 private int v = rng.next();
44 private volatile boolean completed;
45 private volatile int result = 17;
46 private final Mutex lock = new Mutex();
47 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
48 private final CyclicBarrier barrier;
49 private final int nthreads;
50 MutexLoop(int nthreads) {
51 this.nthreads = nthreads;
52 barrier = new CyclicBarrier(nthreads+1, timer);
53 }
54
55 final void test() throws Exception {
56 for (int i = 0; i < nthreads; ++i)
57 pool.execute(this);
58 barrier.await();
59 Thread.sleep(TIMEOUT);
60 lock.lock();
61 barrier.await();
62 if (print) {
63 long time = timer.getTime();
64 double secs = (double) time / 1000000000.0;
65 System.out.println("\t " + secs + "s run time");
66 }
67
68 if (completed)
69 throw new Error("Some thread completed instead of timing out");
70 int r = result;
71 if (r == 0) // avoid overoptimization
72 System.out.println("useless result: " + r);
73 }
74
75 public final void run() {
76 try {
77 barrier.await();
78 int sum = v;
79 int x = 0;
80 int n = ITERS;
81 do {
82 if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
83 break;
84 try {
85 v = x = LoopHelpers.compute1(v);
86 }
87 finally {
88 lock.unlock();
89 }
90 sum += LoopHelpers.compute2(x);
91 } while (n-- > 0);
92 if (n <= 0)
93 completed = true;
94 barrier.await();
95 result += sum;
96 }
97 catch (Exception ie) {
98 return;
99 }
100 }
101 }
102
103 }