< prev index next >

src/java.base/share/classes/java/lang/Thread.java

Print this page
rev 51748 : 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

*** 33,42 **** --- 33,43 ---- import java.security.PrivilegedAction; import java.util.Map; import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; + import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; import jdk.internal.misc.TerminatingThreadLocal; import sun.nio.ch.Interruptible; import jdk.internal.reflect.CallerSensitive;
*** 330,340 **** if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } ! if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } sleep(millis); } --- 331,341 ---- if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } ! if (nanos > 0 && millis < Long.MAX_VALUE) { millis++; } sleep(millis); }
*** 1289,1320 **** * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ ! public final synchronized void join(long millis) throws InterruptedException { ! long base = System.currentTimeMillis(); ! long now = 0; ! ! if (millis < 0) { ! throw new IllegalArgumentException("timeout value is negative"); } ! ! if (millis == 0) { while (isAlive()) { wait(0); } } else { ! while (isAlive()) { ! long delay = millis - now; ! if (delay <= 0) { ! break; ! } ! wait(delay); ! now = System.currentTimeMillis() - base; ! } } } /** * Waits at most {@code millis} milliseconds plus --- 1290,1316 ---- * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ ! public final synchronized void join(final long millis) throws InterruptedException { ! if (millis > 0) { ! if (isAlive()) { ! final long startTime = System.nanoTime(); ! long delay = millis; ! do { ! wait(delay); ! } while (isAlive() && (delay = millis - ! TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) > 0); } ! } else if (millis == 0) { while (isAlive()) { wait(0); } } else { ! throw new IllegalArgumentException("timeout value is negative"); } } /** * Waits at most {@code millis} milliseconds plus
*** 1351,1361 **** if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } ! if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } join(millis); } --- 1347,1357 ---- if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } ! if (nanos > 0 && millis < Long.MAX_VALUE) { millis++; } join(millis); }
< prev index next >