Thread.sleep(0) should not sleep indefinitely

Our implementation uses Object.wait(long) to implement Thread.sleep,
which had the side effect of interpreting zero as infinity.  However,
for Thread.sleep, zero just means zero.  I assume that doesn't mean
"don't sleep at all", though, or else the app wouldn't have called
Thread.sleep in the first place, so this patch sleeps for one
millisecond when zero is passed -- just enough to yield the processor
for a bit.  Thread.yield might be a better choice in this case, but I
assume the app would have called that directly if that's what it
wanted.
This commit is contained in:
Joel Dice 2012-08-12 11:17:59 -06:00
parent f8e860999a
commit 47503854d5
2 changed files with 8 additions and 0 deletions

View File

@ -156,6 +156,10 @@ public class Thread implements Runnable {
}
public static void sleep(long milliseconds) throws InterruptedException {
if (milliseconds <= 0) {
milliseconds = 1;
}
Thread t = currentThread();
if (t.sleepLock == null) {
t.sleepLock = new Object();

View File

@ -3309,6 +3309,10 @@ jvmSleep(Thread* t, uintptr_t* arguments)
{
jlong milliseconds; memcpy(&milliseconds, arguments, sizeof(jlong));
if (milliseconds <= 0) {
milliseconds = 1;
}
if (threadSleepLock(t, t->javaThread) == 0) {
object lock = makeJobject(t);
set(t, t->javaThread, ThreadSleepLock, lock);