44 |
|
void park() { |
45 |
|
LockSupport.park(); |
46 |
|
} |
47 |
< |
void park(long millis) { |
48 |
< |
throw new UnsupportedOperationException(); |
49 |
< |
} |
47 |
> |
Thread.State parkedState() { return Thread.State.WAITING; } |
48 |
|
}, |
49 |
|
parkUntil() { |
50 |
|
void park(long millis) { |
60 |
|
void park() { |
61 |
|
LockSupport.park(theBlocker()); |
62 |
|
} |
63 |
< |
void park(long millis) { |
66 |
< |
throw new UnsupportedOperationException(); |
67 |
< |
} |
63 |
> |
Thread.State parkedState() { return Thread.State.WAITING; } |
64 |
|
}, |
65 |
|
parkUntilBlocker() { |
66 |
|
void park(long millis) { |
75 |
|
}; |
76 |
|
|
77 |
|
void park() { park(2 * LONG_DELAY_MS); } |
78 |
< |
abstract void park(long millis); |
78 |
> |
void park(long millis) { |
79 |
> |
throw new UnsupportedOperationException(); |
80 |
> |
} |
81 |
> |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
82 |
|
|
83 |
|
/** Returns a deadline to use with parkUntil. */ |
84 |
|
long deadline(long millis) { |
185 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
186 |
|
public void realRun() { |
187 |
|
pleaseInterrupt.countDown(); |
188 |
< |
do { |
188 |
> |
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
189 |
|
parkMethod.park(); |
190 |
< |
// park may return spuriously |
191 |
< |
} while (! Thread.currentThread().isInterrupted()); |
190 |
> |
if (Thread.interrupted()) |
191 |
> |
return; |
192 |
> |
} |
193 |
> |
fail("too many consecutive spurious wakeups?"); |
194 |
|
}}); |
195 |
|
|
196 |
|
await(pleaseInterrupt); |
197 |
< |
assertThreadStaysAlive(t); |
197 |
> |
assertThreadBlocks(t, parkMethod.parkedState()); |
198 |
|
t.interrupt(); |
199 |
|
awaitTermination(t); |
200 |
|
} |
222 |
|
} |
223 |
|
public void testParkAfterInterrupt(final ParkMethod parkMethod) { |
224 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
224 |
– |
final AtomicBoolean pleasePark = new AtomicBoolean(false); |
225 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
226 |
|
public void realRun() throws Exception { |
227 |
|
pleaseInterrupt.countDown(); |
228 |
< |
while (!pleasePark.get()) |
228 |
> |
while (!Thread.currentThread().isInterrupted()) |
229 |
|
Thread.yield(); |
230 |
– |
assertTrue(Thread.currentThread().isInterrupted()); |
230 |
|
parkMethod.park(); |
231 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
231 |
> |
assertTrue(Thread.interrupted()); |
232 |
|
}}); |
233 |
|
|
234 |
|
await(pleaseInterrupt); |
235 |
|
t.interrupt(); |
237 |
– |
pleasePark.set(true); |
236 |
|
awaitTermination(t); |
237 |
|
} |
238 |
|
|
257 |
|
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
258 |
|
long startTime = System.nanoTime(); |
259 |
|
parkMethod.park(timeoutMillis()); |
262 |
– |
// park may return spuriously |
260 |
|
if (millisElapsedSince(startTime) >= timeoutMillis()) |
261 |
|
return; |
262 |
|
} |
294 |
|
public void realRun() { |
295 |
|
Thread t = Thread.currentThread(); |
296 |
|
started.countDown(); |
297 |
< |
do { |
297 |
> |
for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) { |
298 |
|
assertNull(LockSupport.getBlocker(t)); |
299 |
|
parkMethod.park(); |
300 |
|
assertNull(LockSupport.getBlocker(t)); |
301 |
< |
// park may return spuriously |
302 |
< |
} while (! Thread.currentThread().isInterrupted()); |
301 |
> |
if (Thread.interrupted()) |
302 |
> |
return; |
303 |
> |
} |
304 |
> |
fail("too many consecutive spurious wakeups?"); |
305 |
|
}}); |
306 |
|
|
307 |
|
long startTime = System.nanoTime(); |
317 |
|
assertNull(x); // ok |
318 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
319 |
|
fail("timed out"); |
320 |
+ |
if (t.getState() == Thread.State.TERMINATED) |
321 |
+ |
break; |
322 |
|
Thread.yield(); |
323 |
|
} |
324 |
|
} |