8098798: Thread.join(ms) on Linux still affected by changes to the time-of-day clock

8210004: Thread.sleep(millis, nanos) timeout returns early

Reviewed-by: martin, igerasim
This commit is contained in:
Roger Riggs 2018-09-14 12:53:07 -04:00
parent bad4a92e55
commit 71b9664abb

View File

@ -35,6 +35,7 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import jdk.internal.misc.TerminatingThreadLocal; import jdk.internal.misc.TerminatingThreadLocal;
@ -332,7 +333,7 @@ class Thread implements Runnable {
"nanosecond timeout value out of range"); "nanosecond timeout value out of range");
} }
if (nanos >= 500000 || (nanos != 0 && millis == 0)) { if (nanos > 0 && millis < Long.MAX_VALUE) {
millis++; millis++;
} }
@ -1291,28 +1292,23 @@ class Thread implements Runnable {
* <i>interrupted status</i> of the current thread is * <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown. * cleared when this exception is thrown.
*/ */
public final synchronized void join(long millis) public final synchronized void join(final long millis)
throws InterruptedException { throws InterruptedException {
long base = System.currentTimeMillis(); if (millis > 0) {
long now = 0; if (isAlive()) {
final long startTime = System.nanoTime();
if (millis < 0) { long delay = millis;
throw new IllegalArgumentException("timeout value is negative"); do {
} wait(delay);
} while (isAlive() && (delay = millis -
if (millis == 0) { TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) > 0);
}
} else if (millis == 0) {
while (isAlive()) { while (isAlive()) {
wait(0); wait(0);
} }
} else { } else {
while (isAlive()) { throw new IllegalArgumentException("timeout value is negative");
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
} }
} }
@ -1353,7 +1349,7 @@ class Thread implements Runnable {
"nanosecond timeout value out of range"); "nanosecond timeout value out of range");
} }
if (nanos >= 500000 || (nanos != 0 && millis == 0)) { if (nanos > 0 && millis < Long.MAX_VALUE) {
millis++; millis++;
} }