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 |
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. |
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{ |
16 |
> |
public class LockSupportTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
+ |
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(LockSupportTest.class); |
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 |
> |
do { |
160 |
> |
LockSupport.park(); |
161 |
> |
// park may return spuriously |
162 |
> |
} while (! Thread.currentThread().isInterrupted()); |
163 |
> |
}}); |
164 |
> |
|
165 |
> |
threadStarted.await(); |
166 |
> |
Thread.sleep(SHORT_DELAY_MS); |
167 |
> |
t.interrupt(); |
168 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
169 |
> |
} |
170 |
> |
|
171 |
> |
/** |
172 |
> |
* parkUntil is released by subsequent interrupt |
173 |
> |
*/ |
174 |
> |
public void testParkUntilBeforeInterrupt() throws InterruptedException { |
175 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
176 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
177 |
> |
public void realRun() { |
178 |
> |
long d = new Date().getTime() + LONG_DELAY_MS; |
179 |
> |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
180 |
> |
long t0 = System.nanoTime(); |
181 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
182 |
> |
threadStarted.countDown(); |
183 |
> |
do { |
184 |
> |
LockSupport.parkUntil(d); |
185 |
> |
// parkUntil may return spuriously |
186 |
> |
} while (! Thread.currentThread().isInterrupted()); |
187 |
> |
assertTrue(System.nanoTime() - t0 < nanos); |
188 |
> |
}}); |
189 |
> |
|
190 |
> |
threadStarted.await(); |
191 |
> |
Thread.sleep(SHORT_DELAY_MS); |
192 |
> |
t.interrupt(); |
193 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
194 |
> |
} |
195 |
> |
|
196 |
> |
/** |
197 |
> |
* parkNanos is released by subsequent interrupt |
198 |
> |
*/ |
199 |
> |
public void testParkNanosBeforeInterrupt() throws InterruptedException { |
200 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
201 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
202 |
> |
public void realRun() { |
203 |
> |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
204 |
> |
long t0 = System.nanoTime(); |
205 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
206 |
> |
threadStarted.countDown(); |
207 |
> |
do { |
208 |
> |
LockSupport.parkNanos(nanos); |
209 |
> |
// parkNanos may return spuriously |
210 |
> |
} while (! Thread.currentThread().isInterrupted()); |
211 |
> |
assertTrue(System.nanoTime() - t0 < nanos); |
212 |
> |
}}); |
213 |
> |
|
214 |
> |
threadStarted.await(); |
215 |
> |
Thread.sleep(SHORT_DELAY_MS); |
216 |
> |
t.interrupt(); |
217 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
218 |
> |
} |
219 |
> |
|
220 |
> |
/** |
221 |
> |
* park is released by preceding interrupt |
222 |
> |
*/ |
223 |
> |
public void testParkAfterInterrupt() throws Exception { |
224 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
225 |
> |
final AtomicBoolean unparked = new AtomicBoolean(false); |
226 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
227 |
> |
public void realRun() throws Exception { |
228 |
> |
threadStarted.countDown(); |
229 |
> |
while (!unparked.get()) |
230 |
> |
Thread.yield(); |
231 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
232 |
> |
LockSupport.park(); |
233 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
234 |
> |
}}); |
235 |
> |
|
236 |
> |
threadStarted.await(); |
237 |
> |
t.interrupt(); |
238 |
> |
unparked.set(true); |
239 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
240 |
> |
} |
241 |
> |
|
242 |
> |
/** |
243 |
> |
* parkUntil is released by preceding interrupt |
244 |
> |
*/ |
245 |
> |
public void testParkUntilAfterInterrupt() throws Exception { |
246 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
247 |
> |
final AtomicBoolean unparked = new AtomicBoolean(false); |
248 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
249 |
> |
public void realRun() throws Exception { |
250 |
> |
threadStarted.countDown(); |
251 |
> |
while (!unparked.get()) |
252 |
> |
Thread.yield(); |
253 |
> |
long d = new Date().getTime() + LONG_DELAY_MS; |
254 |
> |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
255 |
> |
long t0 = System.nanoTime(); |
256 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
257 |
> |
LockSupport.parkUntil(d); |
258 |
> |
assertTrue(System.nanoTime() - t0 < nanos); |
259 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
260 |
> |
}}); |
261 |
> |
|
262 |
> |
threadStarted.await(); |
263 |
> |
t.interrupt(); |
264 |
> |
unparked.set(true); |
265 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
< |
* park is released by unpark occurring after park |
269 |
> |
* parkNanos is released by preceding interrupt |
270 |
|
*/ |
271 |
< |
public void testPark() { |
272 |
< |
Thread t = new Thread(new Runnable() { |
273 |
< |
public void run() { |
274 |
< |
try { |
275 |
< |
LockSupport.park(); |
276 |
< |
} catch(Exception e){ |
277 |
< |
threadUnexpectedException(); |
278 |
< |
} |
279 |
< |
} |
280 |
< |
}); |
281 |
< |
try { |
282 |
< |
t.start(); |
283 |
< |
Thread.sleep(SHORT_DELAY_MS); |
284 |
< |
LockSupport.unpark(t); |
285 |
< |
t.join(); |
286 |
< |
} |
287 |
< |
catch(Exception e) { |
288 |
< |
unexpectedException(); |
289 |
< |
} |
290 |
< |
} |
45 |
< |
|
46 |
< |
/** |
47 |
< |
* park is released by unpark occurring before park |
48 |
< |
*/ |
49 |
< |
public void testPark2() { |
50 |
< |
Thread t = new Thread(new Runnable() { |
51 |
< |
public void run() { |
52 |
< |
try { |
53 |
< |
Thread.sleep(SHORT_DELAY_MS); |
54 |
< |
LockSupport.park(); |
55 |
< |
} catch(Exception e){ |
56 |
< |
threadUnexpectedException(); |
57 |
< |
} |
58 |
< |
} |
59 |
< |
}); |
60 |
< |
try { |
61 |
< |
t.start(); |
62 |
< |
LockSupport.unpark(t); |
63 |
< |
t.join(); |
64 |
< |
} |
65 |
< |
catch(Exception e) { |
66 |
< |
unexpectedException(); |
67 |
< |
} |
68 |
< |
} |
69 |
< |
|
70 |
< |
/** |
71 |
< |
* park is released by interrupt |
72 |
< |
*/ |
73 |
< |
public void testPark3() { |
74 |
< |
Thread t = new Thread(new Runnable() { |
75 |
< |
public void run() { |
76 |
< |
try { |
77 |
< |
LockSupport.park(); |
78 |
< |
threadAssertTrue(Thread.interrupted()); |
79 |
< |
} catch(Exception e){ |
80 |
< |
threadUnexpectedException(); |
81 |
< |
} |
82 |
< |
} |
83 |
< |
}); |
84 |
< |
try { |
85 |
< |
t.start(); |
86 |
< |
Thread.sleep(SHORT_DELAY_MS); |
87 |
< |
t.interrupt(); |
88 |
< |
t.join(); |
89 |
< |
} |
90 |
< |
catch(Exception e) { |
91 |
< |
unexpectedException(); |
92 |
< |
} |
93 |
< |
} |
94 |
< |
|
95 |
< |
/** |
96 |
< |
* park returns if interrupted before park |
97 |
< |
*/ |
98 |
< |
public void testPark4() { |
99 |
< |
final ReentrantLock lock = new ReentrantLock(); |
100 |
< |
lock.lock(); |
101 |
< |
Thread t = new Thread(new Runnable() { |
102 |
< |
public void run() { |
103 |
< |
try { |
104 |
< |
lock.lock(); |
105 |
< |
LockSupport.park(); |
106 |
< |
} catch(Exception e){ |
107 |
< |
threadUnexpectedException(); |
108 |
< |
} |
109 |
< |
} |
110 |
< |
}); |
111 |
< |
try { |
112 |
< |
t.start(); |
113 |
< |
t.interrupt(); |
114 |
< |
lock.unlock(); |
115 |
< |
t.join(); |
116 |
< |
} |
117 |
< |
catch(Exception e) { |
118 |
< |
unexpectedException(); |
119 |
< |
} |
271 |
> |
public void testParkNanosAfterInterrupt() throws Exception { |
272 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
273 |
> |
final AtomicBoolean unparked = new AtomicBoolean(false); |
274 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
275 |
> |
public void realRun() throws Exception { |
276 |
> |
threadStarted.countDown(); |
277 |
> |
while (!unparked.get()) |
278 |
> |
Thread.yield(); |
279 |
> |
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
280 |
> |
long t0 = System.nanoTime(); |
281 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
282 |
> |
LockSupport.parkNanos(nanos); |
283 |
> |
assertTrue(System.nanoTime() - t0 < nanos); |
284 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
285 |
> |
}}); |
286 |
> |
|
287 |
> |
threadStarted.await(); |
288 |
> |
t.interrupt(); |
289 |
> |
unparked.set(true); |
290 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
|
* parkNanos times out if not unparked |
295 |
|
*/ |
296 |
< |
public void testParkNanos() { |
297 |
< |
Thread t = new Thread(new Runnable() { |
298 |
< |
public void run() { |
299 |
< |
try { |
300 |
< |
LockSupport.parkNanos(1000); |
301 |
< |
} catch(Exception e){ |
302 |
< |
threadUnexpectedException(); |
303 |
< |
} |
304 |
< |
} |
305 |
< |
}); |
306 |
< |
try { |
307 |
< |
t.start(); |
308 |
< |
t.join(); |
309 |
< |
} |
139 |
< |
catch(Exception e) { |
140 |
< |
unexpectedException(); |
141 |
< |
} |
296 |
> |
public void testParkNanosTimesOut() throws InterruptedException { |
297 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
> |
public void realRun() { |
299 |
> |
for (;;) { |
300 |
> |
long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
301 |
> |
long t0 = System.nanoTime(); |
302 |
> |
LockSupport.parkNanos(timeoutNanos); |
303 |
> |
// parkNanos may return spuriously |
304 |
> |
if (System.nanoTime() - t0 >= timeoutNanos) |
305 |
> |
return; |
306 |
> |
} |
307 |
> |
}}); |
308 |
> |
|
309 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
/** |
314 |
|
* parkUntil times out if not unparked |
315 |
|
*/ |
316 |
< |
public void testParkUntil() { |
317 |
< |
Thread t = new Thread(new Runnable() { |
318 |
< |
public void run() { |
319 |
< |
try { |
320 |
< |
long d = new Date().getTime() + 100; |
321 |
< |
LockSupport.parkUntil(d); |
322 |
< |
} catch(Exception e){ |
323 |
< |
threadUnexpectedException(); |
324 |
< |
} |
325 |
< |
} |
326 |
< |
}); |
327 |
< |
try { |
328 |
< |
t.start(); |
329 |
< |
t.join(); |
330 |
< |
} |
331 |
< |
catch(Exception e) { |
332 |
< |
unexpectedException(); |
333 |
< |
} |
316 |
> |
public void testParkUntilTimesOut() throws InterruptedException { |
317 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
318 |
> |
public void realRun() { |
319 |
> |
for (;;) { |
320 |
> |
long d = new Date().getTime() + SHORT_DELAY_MS; |
321 |
> |
// beware of rounding |
322 |
> |
long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L; |
323 |
> |
long t0 = System.nanoTime(); |
324 |
> |
LockSupport.parkUntil(d); |
325 |
> |
// parkUntil may return spuriously |
326 |
> |
if (System.nanoTime() - t0 >= timeoutNanos) |
327 |
> |
return; |
328 |
> |
} |
329 |
> |
}}); |
330 |
> |
|
331 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
332 |
> |
} |
333 |
> |
|
334 |
> |
/** |
335 |
> |
* parkUntil(0) returns immediately |
336 |
> |
* Requires hotspot fix for: |
337 |
> |
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |
338 |
> |
*/ |
339 |
> |
public void XXXXtestParkUntil0Returns() throws InterruptedException { |
340 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
341 |
> |
public void realRun() { |
342 |
> |
LockSupport.parkUntil(0L); |
343 |
> |
}}); |
344 |
> |
|
345 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
346 |
|
} |
347 |
|
} |