< 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


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.WeakReference;
  31 import java.security.AccessController;
  32 import java.security.AccessControlContext;
  33 import java.security.PrivilegedAction;
  34 import java.util.Map;
  35 import java.util.HashMap;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.concurrent.ConcurrentMap;

  38 import java.util.concurrent.locks.LockSupport;
  39 
  40 import jdk.internal.misc.TerminatingThreadLocal;
  41 import sun.nio.ch.Interruptible;
  42 import jdk.internal.reflect.CallerSensitive;
  43 import jdk.internal.reflect.Reflection;
  44 import sun.security.util.SecurityConstants;
  45 import jdk.internal.HotSpotIntrinsicCandidate;
  46 
  47 /**
  48  * A <i>thread</i> is a thread of execution in a program. The Java
  49  * Virtual Machine allows an application to have multiple threads of
  50  * execution running concurrently.
  51  * <p>
  52  * Every thread has a priority. Threads with higher priority are
  53  * executed in preference to threads with lower priority. Each thread
  54  * may or may not also be marked as a daemon. When code running in
  55  * some thread creates a new {@code Thread} object, the new
  56  * thread has its priority initially set equal to the priority of the
  57  * creating thread, and is a daemon thread if and only if the


 315      * @throws  IllegalArgumentException
 316      *          if the value of {@code millis} is negative, or the value of
 317      *          {@code nanos} is not in the range {@code 0-999999}
 318      *
 319      * @throws  InterruptedException
 320      *          if any thread has interrupted the current thread. The
 321      *          <i>interrupted status</i> of the current thread is
 322      *          cleared when this exception is thrown.
 323      */
 324     public static void sleep(long millis, int nanos)
 325     throws InterruptedException {
 326         if (millis < 0) {
 327             throw new IllegalArgumentException("timeout value is negative");
 328         }
 329 
 330         if (nanos < 0 || nanos > 999999) {
 331             throw new IllegalArgumentException(
 332                                 "nanosecond timeout value out of range");
 333         }
 334 
 335         if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
 336             millis++;
 337         }
 338 
 339         sleep(millis);
 340     }
 341 
 342     /**
 343      * Indicates that the caller is momentarily unable to progress, until the
 344      * occurrence of one or more actions on the part of other activities. By
 345      * invoking this method within each iteration of a spin-wait loop construct,
 346      * the calling thread indicates to the runtime that it is busy-waiting.
 347      * The runtime may take action to improve the performance of invoking
 348      * spin-wait loop constructions.
 349      *
 350      * @apiNote
 351      * As an example consider a method in a class that spins in a loop until
 352      * some flag is set outside of that method. A call to the {@code onSpinWait}
 353      * method should be placed inside the spin loop.
 354      * <pre>{@code
 355      *     class EventHandler {


1274      * Waits at most {@code millis} milliseconds for this thread to
1275      * die. A timeout of {@code 0} means to wait forever.
1276      *
1277      * <p> This implementation uses a loop of {@code this.wait} calls
1278      * conditioned on {@code this.isAlive}. As a thread terminates the
1279      * {@code this.notifyAll} method is invoked. It is recommended that
1280      * applications not use {@code wait}, {@code notify}, or
1281      * {@code notifyAll} on {@code Thread} instances.
1282      *
1283      * @param  millis
1284      *         the time to wait in milliseconds
1285      *
1286      * @throws  IllegalArgumentException
1287      *          if the value of {@code millis} is negative
1288      *
1289      * @throws  InterruptedException
1290      *          if any thread has interrupted the current thread. The
1291      *          <i>interrupted status</i> of the current thread is
1292      *          cleared when this exception is thrown.
1293      */
1294     public final synchronized void join(long millis)
1295     throws InterruptedException {
1296         long base = System.currentTimeMillis();
1297         long now = 0;
1298 
1299         if (millis < 0) {
1300             throw new IllegalArgumentException("timeout value is negative");



1301         }
1302 
1303         if (millis == 0) {
1304             while (isAlive()) {
1305                 wait(0);
1306             }
1307         } else {
1308             while (isAlive()) {
1309                 long delay = millis - now;
1310                 if (delay <= 0) {
1311                     break;
1312                 }
1313                 wait(delay);
1314                 now = System.currentTimeMillis() - base;
1315             }
1316         }
1317     }
1318 
1319     /**
1320      * Waits at most {@code millis} milliseconds plus
1321      * {@code nanos} nanoseconds for this thread to die.
1322      *
1323      * <p> This implementation uses a loop of {@code this.wait} calls
1324      * conditioned on {@code this.isAlive}. As a thread terminates the
1325      * {@code this.notifyAll} method is invoked. It is recommended that
1326      * applications not use {@code wait}, {@code notify}, or
1327      * {@code notifyAll} on {@code Thread} instances.
1328      *
1329      * @param  millis
1330      *         the time to wait in milliseconds
1331      *
1332      * @param  nanos
1333      *         {@code 0-999999} additional nanoseconds to wait
1334      *
1335      * @throws  IllegalArgumentException
1336      *          if the value of {@code millis} is negative, or the value
1337      *          of {@code nanos} is not in the range {@code 0-999999}
1338      *
1339      * @throws  InterruptedException
1340      *          if any thread has interrupted the current thread. The
1341      *          <i>interrupted status</i> of the current thread is
1342      *          cleared when this exception is thrown.
1343      */
1344     public final synchronized void join(long millis, int nanos)
1345     throws InterruptedException {
1346 
1347         if (millis < 0) {
1348             throw new IllegalArgumentException("timeout value is negative");
1349         }
1350 
1351         if (nanos < 0 || nanos > 999999) {
1352             throw new IllegalArgumentException(
1353                                 "nanosecond timeout value out of range");
1354         }
1355 
1356         if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
1357             millis++;
1358         }
1359 
1360         join(millis);
1361     }
1362 
1363     /**
1364      * Waits for this thread to die.
1365      *
1366      * <p> An invocation of this method behaves in exactly the same
1367      * way as the invocation
1368      *
1369      * <blockquote>
1370      * {@linkplain #join(long) join}{@code (0)}
1371      * </blockquote>
1372      *
1373      * @throws  InterruptedException
1374      *          if any thread has interrupted the current thread. The
1375      *          <i>interrupted status</i> of the current thread is
1376      *          cleared when this exception is thrown.




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.WeakReference;
  31 import java.security.AccessController;
  32 import java.security.AccessControlContext;
  33 import java.security.PrivilegedAction;
  34 import java.util.Map;
  35 import java.util.HashMap;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.concurrent.ConcurrentMap;
  38 import java.util.concurrent.TimeUnit;
  39 import java.util.concurrent.locks.LockSupport;
  40 
  41 import jdk.internal.misc.TerminatingThreadLocal;
  42 import sun.nio.ch.Interruptible;
  43 import jdk.internal.reflect.CallerSensitive;
  44 import jdk.internal.reflect.Reflection;
  45 import sun.security.util.SecurityConstants;
  46 import jdk.internal.HotSpotIntrinsicCandidate;
  47 
  48 /**
  49  * A <i>thread</i> is a thread of execution in a program. The Java
  50  * Virtual Machine allows an application to have multiple threads of
  51  * execution running concurrently.
  52  * <p>
  53  * Every thread has a priority. Threads with higher priority are
  54  * executed in preference to threads with lower priority. Each thread
  55  * may or may not also be marked as a daemon. When code running in
  56  * some thread creates a new {@code Thread} object, the new
  57  * thread has its priority initially set equal to the priority of the
  58  * creating thread, and is a daemon thread if and only if the


 316      * @throws  IllegalArgumentException
 317      *          if the value of {@code millis} is negative, or the value of
 318      *          {@code nanos} is not in the range {@code 0-999999}
 319      *
 320      * @throws  InterruptedException
 321      *          if any thread has interrupted the current thread. The
 322      *          <i>interrupted status</i> of the current thread is
 323      *          cleared when this exception is thrown.
 324      */
 325     public static void sleep(long millis, int nanos)
 326     throws InterruptedException {
 327         if (millis < 0) {
 328             throw new IllegalArgumentException("timeout value is negative");
 329         }
 330 
 331         if (nanos < 0 || nanos > 999999) {
 332             throw new IllegalArgumentException(
 333                                 "nanosecond timeout value out of range");
 334         }
 335 
 336         if (nanos > 0 && millis < Long.MAX_VALUE) {
 337             millis++;
 338         }
 339 
 340         sleep(millis);
 341     }
 342 
 343     /**
 344      * Indicates that the caller is momentarily unable to progress, until the
 345      * occurrence of one or more actions on the part of other activities. By
 346      * invoking this method within each iteration of a spin-wait loop construct,
 347      * the calling thread indicates to the runtime that it is busy-waiting.
 348      * The runtime may take action to improve the performance of invoking
 349      * spin-wait loop constructions.
 350      *
 351      * @apiNote
 352      * As an example consider a method in a class that spins in a loop until
 353      * some flag is set outside of that method. A call to the {@code onSpinWait}
 354      * method should be placed inside the spin loop.
 355      * <pre>{@code
 356      *     class EventHandler {


1275      * Waits at most {@code millis} milliseconds for this thread to
1276      * die. A timeout of {@code 0} means to wait forever.
1277      *
1278      * <p> This implementation uses a loop of {@code this.wait} calls
1279      * conditioned on {@code this.isAlive}. As a thread terminates the
1280      * {@code this.notifyAll} method is invoked. It is recommended that
1281      * applications not use {@code wait}, {@code notify}, or
1282      * {@code notifyAll} on {@code Thread} instances.
1283      *
1284      * @param  millis
1285      *         the time to wait in milliseconds
1286      *
1287      * @throws  IllegalArgumentException
1288      *          if the value of {@code millis} is negative
1289      *
1290      * @throws  InterruptedException
1291      *          if any thread has interrupted the current thread. The
1292      *          <i>interrupted status</i> of the current thread is
1293      *          cleared when this exception is thrown.
1294      */
1295     public final synchronized void join(final long millis)
1296     throws InterruptedException {
1297         if (millis > 0) {
1298             if (isAlive()) {
1299                 final long startTime = System.nanoTime();
1300                 long delay = millis;
1301                 do {
1302                     wait(delay);
1303                 } while (isAlive() && (delay = millis -
1304                         TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) > 0);
1305             }
1306         } else if (millis == 0) {

1307             while (isAlive()) {
1308                 wait(0);
1309             }
1310         } else {
1311             throw new IllegalArgumentException("timeout value is negative");







1312         }
1313     }
1314 
1315     /**
1316      * Waits at most {@code millis} milliseconds plus
1317      * {@code nanos} nanoseconds for this thread to die.
1318      *
1319      * <p> This implementation uses a loop of {@code this.wait} calls
1320      * conditioned on {@code this.isAlive}. As a thread terminates the
1321      * {@code this.notifyAll} method is invoked. It is recommended that
1322      * applications not use {@code wait}, {@code notify}, or
1323      * {@code notifyAll} on {@code Thread} instances.
1324      *
1325      * @param  millis
1326      *         the time to wait in milliseconds
1327      *
1328      * @param  nanos
1329      *         {@code 0-999999} additional nanoseconds to wait
1330      *
1331      * @throws  IllegalArgumentException
1332      *          if the value of {@code millis} is negative, or the value
1333      *          of {@code nanos} is not in the range {@code 0-999999}
1334      *
1335      * @throws  InterruptedException
1336      *          if any thread has interrupted the current thread. The
1337      *          <i>interrupted status</i> of the current thread is
1338      *          cleared when this exception is thrown.
1339      */
1340     public final synchronized void join(long millis, int nanos)
1341     throws InterruptedException {
1342 
1343         if (millis < 0) {
1344             throw new IllegalArgumentException("timeout value is negative");
1345         }
1346 
1347         if (nanos < 0 || nanos > 999999) {
1348             throw new IllegalArgumentException(
1349                                 "nanosecond timeout value out of range");
1350         }
1351 
1352         if (nanos > 0 && millis < Long.MAX_VALUE) {
1353             millis++;
1354         }
1355 
1356         join(millis);
1357     }
1358 
1359     /**
1360      * Waits for this thread to die.
1361      *
1362      * <p> An invocation of this method behaves in exactly the same
1363      * way as the invocation
1364      *
1365      * <blockquote>
1366      * {@linkplain #join(long) join}{@code (0)}
1367      * </blockquote>
1368      *
1369      * @throws  InterruptedException
1370      *          if any thread has interrupted the current thread. The
1371      *          <i>interrupted status</i> of the current thread is
1372      *          cleared when this exception is thrown.


< prev index next >