Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.java
+++ new/test/java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.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
37 - * @compile CancelledLockLoops.java
37 + * @compile -source 1.5 CancelledLockLoops.java
38 38 * @run main/timeout=2800 CancelledLockLoops
39 39 * @summary tests lockInterruptibly.
40 40 * Checks for responsiveness of locks to interrupts. Runs under that
41 41 * assumption that ITERS_VALUE computations require more than TIMEOUT
42 42 * msecs to complete.
43 43 */
44 44
45 45 import java.util.concurrent.*;
46 46 import java.util.concurrent.locks.*;
47 47 import java.util.*;
48 48
49 49 public final class CancelledLockLoops {
50 50 static final Random rng = new Random();
51 51 static boolean print = false;
52 52 static final int ITERS = 1000000;
53 53 static final long TIMEOUT = 100;
54 54
55 55 public static void main(String[] args) throws Exception {
56 56 int maxThreads = 5;
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
57 57 if (args.length > 0)
58 58 maxThreads = Integer.parseInt(args[0]);
59 59
60 60 print = true;
61 61
62 62 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
63 63 System.out.print("Threads: " + i);
64 64 try {
65 65 new ReentrantLockLoop(i).test();
66 66 }
67 - catch(BrokenBarrierException bb) {
67 + catch (BrokenBarrierException bb) {
68 68 // OK, ignore
69 69 }
70 70 Thread.sleep(TIMEOUT);
71 71 }
72 72 }
73 73
74 74 static final class ReentrantLockLoop implements Runnable {
75 75 private int v = rng.nextInt();
76 76 private int completed;
77 77 private volatile int result = 17;
78 78 private final ReentrantLock lock = new ReentrantLock();
79 79 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
80 80 private final CyclicBarrier barrier;
81 81 private final int nthreads;
82 82 ReentrantLockLoop(int nthreads) {
83 83 this.nthreads = nthreads;
84 84 barrier = new CyclicBarrier(nthreads+1, timer);
85 85 }
86 86
87 87 final void test() throws Exception {
88 88 Thread[] threads = new Thread[nthreads];
89 89 for (int i = 0; i < threads.length; ++i)
90 90 threads[i] = new Thread(this);
91 91 for (int i = 0; i < threads.length; ++i)
92 92 threads[i].start();
93 93 Thread[] cancels = (Thread[]) (threads.clone());
94 94 Collections.shuffle(Arrays.asList(cancels), rng);
95 95 barrier.await();
96 96 Thread.sleep(TIMEOUT);
97 97 for (int i = 0; i < cancels.length-2; ++i) {
98 98 cancels[i].interrupt();
99 99 // make sure all OK even when cancellations spaced out
100 100 if ( (i & 3) == 0)
101 101 Thread.sleep(1 + rng.nextInt(10));
102 102 }
103 103 barrier.await();
104 104 if (print) {
105 105 long time = timer.getTime();
106 106 double secs = (double)(time) / 1000000000.0;
107 107 System.out.println("\t " + secs + "s run time");
108 108 }
109 109
110 110 int c;
111 111 lock.lock();
112 112 try {
113 113 c = completed;
114 114 }
115 115 finally {
116 116 lock.unlock();
117 117 }
118 118 if (c != 2)
119 119 throw new Error("Completed != 2");
120 120 int r = result;
121 121 if (r == 0) // avoid overoptimization
122 122 System.out.println("useless result: " + r);
123 123 }
124 124
125 125 public final void run() {
126 126 try {
127 127 barrier.await();
128 128 int sum = v;
129 129 int x = 0;
130 130 int n = ITERS;
131 131 boolean done = false;
132 132 do {
133 133 try {
134 134 lock.lockInterruptibly();
135 135 }
136 136 catch (InterruptedException ie) {
137 137 break;
138 138 }
139 139 try {
140 140 v = x = LoopHelpers.compute1(v);
141 141 }
142 142 finally {
143 143 lock.unlock();
144 144 }
145 145 sum += LoopHelpers.compute2(x);
146 146 } while (n-- > 0);
147 147 if (n <= 0) {
148 148 lock.lock();
149 149 try {
150 150 ++completed;
151 151 }
152 152 finally {
153 153 lock.unlock();
154 154 }
155 155 }
156 156 barrier.await();
157 157 result += sum;
158 158 }
159 159 catch (Exception ex) {
160 160 ex.printStackTrace();
161 161 return;
162 162 }
163 163 }
164 164 }
165 165
166 166 }
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX