8309408: Thread.sleep cleanup

Reviewed-by: dholmes, stefank
This commit is contained in:
Alan Bateman 2023-06-16 07:26:27 +00:00
parent 32243ef47d
commit 227656f3a9
10 changed files with 45 additions and 48 deletions

View file

@ -478,6 +478,25 @@ public class Thread implements Runnable {
}
}
/**
* Sleep for the specified number of nanoseconds, subject to the precision
* and accuracy of system timers and schedulers.
*/
private static void sleepNanos(long nanos) throws InterruptedException {
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleepNanos0(nanos);
}
} finally {
afterSleep(event);
}
}
private static native void sleepNanos0(long nanos) throws InterruptedException;
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
@ -499,22 +518,10 @@ public class Thread implements Runnable {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
long nanos = MILLISECONDS.toNanos(millis);
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleep0(nanos);
}
} finally {
afterSleep(event);
}
sleepNanos(nanos);
}
private static native void sleep0(long nanos) throws InterruptedException;
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds plus the specified
@ -549,17 +556,7 @@ public class Thread implements Runnable {
// total sleep time, in nanoseconds
long totalNanos = MILLISECONDS.toNanos(millis);
totalNanos += Math.min(Long.MAX_VALUE - totalNanos, nanos);
ThreadSleepEvent event = beforeSleep(totalNanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(totalNanos);
} else {
sleep0(totalNanos);
}
} finally {
afterSleep(event);
}
sleepNanos(totalNanos);
}
/**
@ -583,17 +580,7 @@ public class Thread implements Runnable {
if (nanos < 0) {
return;
}
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleep0(nanos);
}
} finally {
afterSleep(event);
}
sleepNanos(nanos);
}
/**