ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutLockLoops.java
Revision: 1.7
Committed: Tue Jan 14 02:43:14 2014 UTC (10 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +0 -1 lines
Log Message:
remove all occurrences of -source 1.5

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 * @bug 4486658
9 * @run main TimeoutLockLoops
10 * @summary Checks for responsiveness of locks to timeouts.
11 * Runs under the assumption that ITERS computations require more than
12 * TIMEOUT msecs to complete, which seems to be a safe assumption for
13 * another decade.
14 */
15
16 import java.util.concurrent.*;
17 import java.util.concurrent.locks.*;
18 import java.util.*;
19
20 public final class TimeoutLockLoops {
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 ReentrantLockLoop(i).test();
37 // Thread.sleep(10);
38 }
39 pool.shutdown();
40 }
41
42 static final class ReentrantLockLoop implements Runnable {
43 private int v = rng.next();
44 private volatile boolean completed;
45 private volatile int result = 17;
46 private final ReentrantLock lock = new ReentrantLock();
47 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
48 private final CyclicBarrier barrier;
49 private final int nthreads;
50 ReentrantLockLoop(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 lock.lock();
58 pool.execute(this);
59 lock.unlock();
60 }
61 barrier.await();
62 Thread.sleep(TIMEOUT);
63 while (!lock.tryLock()); // Jam lock
64 // lock.lock();
65 barrier.await();
66 if (print) {
67 long time = timer.getTime();
68 double secs = (double) time / 1000000000.0;
69 System.out.println("\t " + secs + "s run time");
70 }
71
72 if (completed)
73 throw new Error("Some thread completed instead of timing out");
74 int r = result;
75 if (r == 0) // avoid overoptimization
76 System.out.println("useless result: " + r);
77 }
78
79 public final void run() {
80 try {
81 barrier.await();
82 int sum = v;
83 int x = 17;
84 int n = ITERS;
85 final ReentrantLock lock = this.lock;
86 for (;;) {
87 if (x != 0) {
88 if (n-- <= 0)
89 break;
90 }
91 if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
92 break;
93 try {
94 v = x = LoopHelpers.compute1(v);
95 }
96 finally {
97 lock.unlock();
98 }
99 sum += LoopHelpers.compute2(x);
100 }
101 if (n <= 0)
102 completed = true;
103 barrier.await();
104 result += sum;
105 }
106 catch (Exception ex) {
107 ex.printStackTrace();
108 return;
109 }
110 }
111 }
112
113
114 }