mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8305092: Improve Thread.sleep(millis, nanos) for sub-millisecond granularity
Reviewed-by: dholmes, alanb
This commit is contained in:
parent
891530fbc9
commit
fcb280a48b
11 changed files with 245 additions and 28 deletions
|
@ -506,14 +506,14 @@ public class Thread implements Runnable {
|
|||
if (currentThread() instanceof VirtualThread vthread) {
|
||||
vthread.sleepNanos(nanos);
|
||||
} else {
|
||||
sleep0(millis);
|
||||
sleep0(nanos);
|
||||
}
|
||||
} finally {
|
||||
afterSleep(event);
|
||||
}
|
||||
}
|
||||
|
||||
private static native void sleep0(long millis) throws InterruptedException;
|
||||
private static native void sleep0(long nanos) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Causes the currently executing thread to sleep (temporarily cease
|
||||
|
@ -555,11 +555,7 @@ public class Thread implements Runnable {
|
|||
if (currentThread() instanceof VirtualThread vthread) {
|
||||
vthread.sleepNanos(totalNanos);
|
||||
} else {
|
||||
// millisecond precision
|
||||
if (nanos > 0 && millis < Long.MAX_VALUE) {
|
||||
millis++;
|
||||
}
|
||||
sleep0(millis);
|
||||
sleep0(totalNanos);
|
||||
}
|
||||
} finally {
|
||||
afterSleep(event);
|
||||
|
@ -593,12 +589,7 @@ public class Thread implements Runnable {
|
|||
if (currentThread() instanceof VirtualThread vthread) {
|
||||
vthread.sleepNanos(nanos);
|
||||
} else {
|
||||
// millisecond precision
|
||||
long millis = NANOSECONDS.toMillis(nanos);
|
||||
if (nanos > MILLISECONDS.toNanos(millis)) {
|
||||
millis += 1L;
|
||||
}
|
||||
sleep0(millis);
|
||||
sleep0(nanos);
|
||||
}
|
||||
} finally {
|
||||
afterSleep(event);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue