1299 |
|
startTime = System.nanoTime(); |
1300 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1301 |
|
threadAssertTrue(thread.isAlive()); |
1302 |
< |
return; |
1302 |
> |
fail("timed out waiting for thread to enter wait state"); |
1303 |
|
} |
1304 |
|
Thread.yield(); |
1305 |
|
} |
1306 |
|
} |
1307 |
|
|
1308 |
|
/** |
1309 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1310 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1309 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1310 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1311 |
> |
* and additionally satisfy the given condition. |
1312 |
> |
*/ |
1313 |
> |
void waitForThreadToEnterWaitState( |
1314 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1315 |
> |
long startTime = 0L; |
1316 |
> |
for (;;) { |
1317 |
> |
Thread.State s = thread.getState(); |
1318 |
> |
if (s == Thread.State.BLOCKED || |
1319 |
> |
s == Thread.State.WAITING || |
1320 |
> |
s == Thread.State.TIMED_WAITING) { |
1321 |
> |
try { |
1322 |
> |
if (waitingForGodot.call()) |
1323 |
> |
return; |
1324 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1325 |
> |
} |
1326 |
> |
else if (s == Thread.State.TERMINATED) |
1327 |
> |
fail("Unexpected thread termination"); |
1328 |
> |
else if (startTime == 0L) |
1329 |
> |
startTime = System.nanoTime(); |
1330 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1331 |
> |
threadAssertTrue(thread.isAlive()); |
1332 |
> |
fail("timed out waiting for thread to enter wait state"); |
1333 |
> |
} |
1334 |
> |
Thread.yield(); |
1335 |
> |
} |
1336 |
> |
} |
1337 |
> |
|
1338 |
> |
/** |
1339 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1340 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1341 |
|
*/ |
1342 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1343 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1344 |
|
} |
1345 |
|
|
1346 |
|
/** |
1347 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1348 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1349 |
+ |
* and additionally satisfy the given condition. |
1350 |
+ |
*/ |
1351 |
+ |
void waitForThreadToEnterWaitState( |
1352 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1353 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1354 |
+ |
} |
1355 |
+ |
|
1356 |
+ |
/** |
1357 |
|
* Returns the number of milliseconds since time given by |
1358 |
|
* startNanoTime, which must have been previously returned from a |
1359 |
|
* call to {@link System#nanoTime()}. |