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 |
5 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
34 |
|
}}); |
35 |
|
|
36 |
|
threadStarted.await(); |
37 |
< |
Thread.sleep(SHORT_DELAY_MS); |
37 |
> |
delay(SHORT_DELAY_MS); |
38 |
|
LockSupport.unpark(t); |
39 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
40 |
|
} |
55 |
|
}}); |
56 |
|
|
57 |
|
threadStarted.await(); |
58 |
< |
Thread.sleep(SHORT_DELAY_MS); |
58 |
> |
delay(SHORT_DELAY_MS); |
59 |
|
LockSupport.unpark(t); |
60 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
61 |
|
} |
75 |
|
}}); |
76 |
|
|
77 |
|
threadStarted.await(); |
78 |
< |
Thread.sleep(SHORT_DELAY_MS); |
78 |
> |
delay(SHORT_DELAY_MS); |
79 |
|
LockSupport.unpark(t); |
80 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
81 |
|
} |
156 |
|
public void realRun() { |
157 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
158 |
|
threadStarted.countDown(); |
159 |
< |
LockSupport.park(); |
160 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
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); |
166 |
> |
delay(SHORT_DELAY_MS); |
167 |
|
t.interrupt(); |
168 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
169 |
|
} |
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 |
< |
LockSupport.parkUntil(d); |
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); |
191 |
> |
delay(SHORT_DELAY_MS); |
192 |
|
t.interrupt(); |
193 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
194 |
|
} |
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 |
< |
LockSupport.parkNanos(nanos); |
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); |
215 |
> |
delay(SHORT_DELAY_MS); |
216 |
|
t.interrupt(); |
217 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
218 |
|
} |
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(); |
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(); |
296 |
|
public void testParkNanosTimesOut() throws InterruptedException { |
297 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
|
public void realRun() { |
299 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
300 |
< |
long t0 = System.nanoTime(); |
301 |
< |
LockSupport.parkNanos(timeoutNanos); |
302 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
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); |
316 |
|
public void testParkUntilTimesOut() throws InterruptedException { |
317 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
318 |
|
public void realRun() { |
319 |
< |
long d = new Date().getTime() + SHORT_DELAY_MS; |
320 |
< |
// beware of rounding |
321 |
< |
long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L; |
322 |
< |
long t0 = System.nanoTime(); |
323 |
< |
LockSupport.parkUntil(d); |
324 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
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 |
+ |
* getBlocker(null) throws NullPointerException |
336 |
+ |
*/ |
337 |
+ |
public void testGetBlockerNull() { |
338 |
+ |
try { |
339 |
+ |
LockSupport.getBlocker(null); |
340 |
+ |
shouldThrow(); |
341 |
+ |
} catch (NullPointerException success) {} |
342 |
+ |
} |
343 |
+ |
|
344 |
+ |
/** |
345 |
|
* parkUntil(0) returns immediately |
346 |
|
* Requires hotspot fix for: |
347 |
|
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |