Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/locks/ReentrantLock/TimeoutLockLoops.java
+++ new/test/java/util/concurrent/locks/ReentrantLock/TimeoutLockLoops.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation.
7 7 *
8 8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 11 * version 2 for more details (a copy is included in the LICENSE file that
12 12 * accompanied this code).
13 13 *
14 14 * You should have received a copy of the GNU General Public License version
15 15 * 2 along with this work; if not, write to the Free Software Foundation,
16 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 17 *
18 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 19 * or visit www.oracle.com if you need additional information or have any
20 20 * questions.
21 21 */
22 22
23 23 /*
24 24 * This file is available under and governed by the GNU General Public
25 25 * License version 2 only, as published by the Free Software Foundation.
26 26 * However, the following notice accompanied the original version of this
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
27 27 * file:
28 28 *
29 29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 30 * Expert Group and released to the public domain, as explained at
31 31 * http://creativecommons.org/licenses/publicdomain
32 32 */
33 33
34 34 /*
35 35 * @test
36 36 * @bug 4486658 5031862
37 + * @compile -source 1.5 TimeoutLockLoops.java
38 + * @run main TimeoutLockLoops
37 39 * @summary Checks for responsiveness of locks to timeouts.
38 40 * Runs under the assumption that ITERS computations require more than
39 41 * TIMEOUT msecs to complete, which seems to be a safe assumption for
40 42 * another decade.
41 43 */
42 44
43 45 import java.util.concurrent.*;
44 46 import java.util.concurrent.locks.*;
45 47 import java.util.*;
46 48
47 49 public final class TimeoutLockLoops {
48 50 static final ExecutorService pool = Executors.newCachedThreadPool();
49 51 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
50 52 static boolean print = false;
51 53 static final int ITERS = Integer.MAX_VALUE;
52 54 static final long TIMEOUT = 100;
53 55
54 56 public static void main(String[] args) throws Exception {
55 57 int maxThreads = 100;
56 58 if (args.length > 0)
57 59 maxThreads = Integer.parseInt(args[0]);
58 60
59 61
60 62 print = true;
61 63
62 64 for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
63 65 System.out.print("Threads: " + i);
64 66 new ReentrantLockLoop(i).test();
65 67 Thread.sleep(10);
66 68 }
67 69 pool.shutdown();
68 70 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
69 71 throw new Error();
70 72 }
71 73
72 74 static final class ReentrantLockLoop implements Runnable {
73 75 private int v = rng.next();
74 76 private volatile boolean completed;
75 77 private volatile int result = 17;
76 78 private final ReentrantLock lock = new ReentrantLock();
77 79 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
78 80 private final CyclicBarrier barrier;
79 81 private final int nthreads;
80 82 ReentrantLockLoop(int nthreads) {
81 83 this.nthreads = nthreads;
82 84 barrier = new CyclicBarrier(nthreads+1, timer);
83 85 }
84 86
85 87 final void test() throws Exception {
86 88 for (int i = 0; i < nthreads; ++i) {
87 89 lock.lock();
88 90 pool.execute(this);
89 91 lock.unlock();
90 92 }
91 93 barrier.await();
92 94 Thread.sleep(TIMEOUT);
93 95 while (!lock.tryLock()); // Jam lock
94 96 // lock.lock();
95 97 barrier.await();
96 98 if (print) {
97 99 long time = timer.getTime();
98 100 double secs = (double)(time) / 1000000000.0;
99 101 System.out.println("\t " + secs + "s run time");
100 102 }
101 103
102 104 if (completed)
103 105 throw new Error("Some thread completed instead of timing out");
104 106 int r = result;
105 107 if (r == 0) // avoid overoptimization
106 108 System.out.println("useless result: " + r);
107 109 }
108 110
109 111 public final void run() {
110 112 try {
111 113 barrier.await();
112 114 int sum = v;
113 115 int x = 17;
114 116 int n = ITERS;
115 117 final ReentrantLock lock = this.lock;
116 118 for (;;) {
117 119 if (x != 0) {
118 120 if (n-- <= 0)
119 121 break;
120 122 }
121 123 if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
122 124 break;
123 125 try {
124 126 v = x = LoopHelpers.compute1(v);
125 127 }
126 128 finally {
127 129 lock.unlock();
128 130 }
129 131 sum += LoopHelpers.compute2(x);
130 132 }
131 133 if (n <= 0)
132 134 completed = true;
133 135 barrier.await();
134 136 result += sum;
135 137 }
136 138 catch (Exception ex) {
137 139 ex.printStackTrace();
138 140 return;
139 141 }
140 142 }
141 143 }
142 144
143 145
144 146 }
↓ open down ↓ |
98 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX