ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.16
Committed: Wed Oct 6 04:05:42 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +31 -54 lines
Log Message:
use newStartedThread and awaitTermination

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/licenses/publicdomain
6 * Other contributors include Andrew Wright, Jeffrey Hayes,
7 * Pat Fisher, Mike Judd.
8 */
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import java.util.concurrent.atomic.AtomicBoolean;
14 import java.util.concurrent.locks.*;
15
16 public class LockSupportTest extends JSR166TestCase {
17 public static void main(String[] args) {
18 junit.textui.TestRunner.run(suite());
19 }
20
21 public static Test suite() {
22 return new TestSuite(LockSupportTest.class);
23 }
24
25 /**
26 * park is released by subsequent unpark
27 */
28 public void testParkBeforeUnpark() throws InterruptedException {
29 final CountDownLatch threadStarted = new CountDownLatch(1);
30 Thread t = newStartedThread(new CheckedRunnable() {
31 public void realRun() {
32 threadStarted.countDown();
33 LockSupport.park();
34 }});
35
36 threadStarted.await();
37 Thread.sleep(SHORT_DELAY_MS);
38 LockSupport.unpark(t);
39 awaitTermination(t, MEDIUM_DELAY_MS);
40 }
41
42 /**
43 * parkUntil is released by subsequent unpark
44 */
45 public void testParkUntilBeforeUnpark() throws InterruptedException {
46 final CountDownLatch threadStarted = new CountDownLatch(1);
47 Thread t = newStartedThread(new CheckedRunnable() {
48 public void realRun() {
49 long d = new Date().getTime() + LONG_DELAY_MS;
50 long nanos = LONG_DELAY_MS * 1000L * 1000L;
51 long t0 = System.nanoTime();
52 threadStarted.countDown();
53 LockSupport.parkUntil(d);
54 assertTrue(System.nanoTime() - t0 < nanos);
55 }});
56
57 threadStarted.await();
58 Thread.sleep(SHORT_DELAY_MS);
59 LockSupport.unpark(t);
60 awaitTermination(t, MEDIUM_DELAY_MS);
61 }
62
63 /**
64 * parkNanos is released by subsequent unpark
65 */
66 public void testParkNanosBeforeUnpark() throws InterruptedException {
67 final CountDownLatch threadStarted = new CountDownLatch(1);
68 Thread t = newStartedThread(new CheckedRunnable() {
69 public void realRun() {
70 long nanos = LONG_DELAY_MS * 1000L * 1000L;
71 long t0 = System.nanoTime();
72 threadStarted.countDown();
73 LockSupport.parkNanos(nanos);
74 assertTrue(System.nanoTime() - t0 < nanos);
75 }});
76
77 threadStarted.await();
78 Thread.sleep(SHORT_DELAY_MS);
79 LockSupport.unpark(t);
80 awaitTermination(t, MEDIUM_DELAY_MS);
81 }
82
83 /**
84 * park is released by preceding unpark
85 */
86 public void testParkAfterUnpark() throws Exception {
87 final CountDownLatch threadStarted = new CountDownLatch(1);
88 final AtomicBoolean unparked = new AtomicBoolean(false);
89 Thread t = newStartedThread(new CheckedRunnable() {
90 public void realRun() throws Exception {
91 threadStarted.countDown();
92 while (!unparked.get())
93 Thread.yield();
94 LockSupport.park();
95 }});
96
97 threadStarted.await();
98 LockSupport.unpark(t);
99 unparked.set(true);
100 awaitTermination(t, MEDIUM_DELAY_MS);
101 }
102
103 /**
104 * parkUntil is released by preceding unpark
105 */
106 public void testParkUntilAfterUnpark() throws Exception {
107 final CountDownLatch threadStarted = new CountDownLatch(1);
108 final AtomicBoolean unparked = new AtomicBoolean(false);
109 Thread t = newStartedThread(new CheckedRunnable() {
110 public void realRun() throws Exception {
111 threadStarted.countDown();
112 while (!unparked.get())
113 Thread.yield();
114 long d = new Date().getTime() + LONG_DELAY_MS;
115 long nanos = LONG_DELAY_MS * 1000L * 1000L;
116 long t0 = System.nanoTime();
117 LockSupport.parkUntil(d);
118 assertTrue(System.nanoTime() - t0 < nanos);
119 }});
120
121 threadStarted.await();
122 LockSupport.unpark(t);
123 unparked.set(true);
124 awaitTermination(t, MEDIUM_DELAY_MS);
125 }
126
127 /**
128 * parkNanos is released by preceding unpark
129 */
130 public void testParkNanosAfterUnpark() throws Exception {
131 final CountDownLatch threadStarted = new CountDownLatch(1);
132 final AtomicBoolean unparked = new AtomicBoolean(false);
133 Thread t = newStartedThread(new CheckedRunnable() {
134 public void realRun() throws Exception {
135 threadStarted.countDown();
136 while (!unparked.get())
137 Thread.yield();
138 long nanos = LONG_DELAY_MS * 1000L * 1000L;
139 long t0 = System.nanoTime();
140 LockSupport.parkNanos(nanos);
141 assertTrue(System.nanoTime() - t0 < nanos);
142 }});
143
144 threadStarted.await();
145 LockSupport.unpark(t);
146 unparked.set(true);
147 awaitTermination(t, MEDIUM_DELAY_MS);
148 }
149
150 /**
151 * park is released by subsequent interrupt
152 */
153 public void testParkBeforeInterrupt() throws InterruptedException {
154 final CountDownLatch threadStarted = new CountDownLatch(1);
155 Thread t = newStartedThread(new CheckedRunnable() {
156 public void realRun() {
157 assertFalse(Thread.currentThread().isInterrupted());
158 threadStarted.countDown();
159 LockSupport.park();
160 assertTrue(Thread.currentThread().isInterrupted());
161 }});
162
163 threadStarted.await();
164 Thread.sleep(SHORT_DELAY_MS);
165 t.interrupt();
166 awaitTermination(t, MEDIUM_DELAY_MS);
167 }
168
169 /**
170 * parkUntil is released by subsequent interrupt
171 */
172 public void testParkUntilBeforeInterrupt() throws InterruptedException {
173 final CountDownLatch threadStarted = new CountDownLatch(1);
174 Thread t = newStartedThread(new CheckedRunnable() {
175 public void realRun() {
176 long d = new Date().getTime() + LONG_DELAY_MS;
177 long nanos = LONG_DELAY_MS * 1000L * 1000L;
178 long t0 = System.nanoTime();
179 threadStarted.countDown();
180 LockSupport.parkUntil(d);
181 assertTrue(System.nanoTime() - t0 < nanos);
182 }});
183
184 threadStarted.await();
185 Thread.sleep(SHORT_DELAY_MS);
186 t.interrupt();
187 awaitTermination(t, MEDIUM_DELAY_MS);
188 }
189
190 /**
191 * parkNanos is released by subsequent interrupt
192 */
193 public void testParkNanosBeforeInterrupt() throws InterruptedException {
194 final CountDownLatch threadStarted = new CountDownLatch(1);
195 Thread t = newStartedThread(new CheckedRunnable() {
196 public void realRun() {
197 long nanos = LONG_DELAY_MS * 1000L * 1000L;
198 long t0 = System.nanoTime();
199 threadStarted.countDown();
200 LockSupport.parkNanos(nanos);
201 assertTrue(System.nanoTime() - t0 < nanos);
202 }});
203
204 threadStarted.await();
205 Thread.sleep(SHORT_DELAY_MS);
206 t.interrupt();
207 awaitTermination(t, MEDIUM_DELAY_MS);
208 }
209
210 /**
211 * park is released by preceding interrupt
212 */
213 public void testParkAfterInterrupt() throws Exception {
214 final CountDownLatch threadStarted = new CountDownLatch(1);
215 final AtomicBoolean unparked = new AtomicBoolean(false);
216 Thread t = newStartedThread(new CheckedRunnable() {
217 public void realRun() throws Exception {
218 threadStarted.countDown();
219 while (!unparked.get())
220 Thread.yield();
221 assertTrue(Thread.currentThread().isInterrupted());
222 LockSupport.park();
223 assertTrue(Thread.currentThread().isInterrupted());
224 }});
225
226 threadStarted.await();
227 t.interrupt();
228 unparked.set(true);
229 awaitTermination(t, MEDIUM_DELAY_MS);
230 }
231
232 /**
233 * parkUntil is released by preceding interrupt
234 */
235 public void testParkUntilAfterInterrupt() throws Exception {
236 final CountDownLatch threadStarted = new CountDownLatch(1);
237 final AtomicBoolean unparked = new AtomicBoolean(false);
238 Thread t = newStartedThread(new CheckedRunnable() {
239 public void realRun() throws Exception {
240 threadStarted.countDown();
241 while (!unparked.get())
242 Thread.yield();
243 long d = new Date().getTime() + LONG_DELAY_MS;
244 long nanos = LONG_DELAY_MS * 1000L * 1000L;
245 long t0 = System.nanoTime();
246 LockSupport.parkUntil(d);
247 assertTrue(System.nanoTime() - t0 < nanos);
248 }});
249
250 threadStarted.await();
251 t.interrupt();
252 unparked.set(true);
253 awaitTermination(t, MEDIUM_DELAY_MS);
254 }
255
256 /**
257 * parkNanos is released by preceding interrupt
258 */
259 public void testParkNanosAfterInterrupt() throws Exception {
260 final CountDownLatch threadStarted = new CountDownLatch(1);
261 final AtomicBoolean unparked = new AtomicBoolean(false);
262 Thread t = newStartedThread(new CheckedRunnable() {
263 public void realRun() throws Exception {
264 threadStarted.countDown();
265 while (!unparked.get())
266 Thread.yield();
267 long nanos = LONG_DELAY_MS * 1000L * 1000L;
268 long t0 = System.nanoTime();
269 LockSupport.parkNanos(nanos);
270 assertTrue(System.nanoTime() - t0 < nanos);
271 }});
272
273 threadStarted.await();
274 t.interrupt();
275 unparked.set(true);
276 awaitTermination(t, MEDIUM_DELAY_MS);
277 }
278
279 /**
280 * parkNanos times out if not unparked
281 */
282 public void testParkNanosTimesOut() throws InterruptedException {
283 Thread t = newStartedThread(new CheckedRunnable() {
284 public void realRun() {
285 final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
286 long t0 = System.nanoTime();
287 LockSupport.parkNanos(timeoutNanos);
288 assertTrue(System.nanoTime() - t0 >= timeoutNanos);
289 }});
290
291 awaitTermination(t, MEDIUM_DELAY_MS);
292 }
293
294
295 /**
296 * parkUntil times out if not unparked
297 */
298 public void testParkUntilTimesOut() throws InterruptedException {
299 Thread t = newStartedThread(new CheckedRunnable() {
300 public void realRun() {
301 long d = new Date().getTime() + SHORT_DELAY_MS;
302 // beware of rounding
303 long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L;
304 long t0 = System.nanoTime();
305 LockSupport.parkUntil(d);
306 assertTrue(System.nanoTime() - t0 >= timeoutNanos);
307 }});
308
309 awaitTermination(t, MEDIUM_DELAY_MS);
310 }
311
312 /**
313 * parkUntil(0) returns immediately
314 * Requires hotspot fix for:
315 * 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
316 */
317 public void XXXXtestParkUntil0Returns() throws InterruptedException {
318 Thread t = newStartedThread(new CheckedRunnable() {
319 public void realRun() {
320 LockSupport.parkUntil(0L);
321 }});
322
323 awaitTermination(t, MEDIUM_DELAY_MS);
324 }
325 }