From 3e68a8af37f521fdfbd5815cea537a4ab405003b Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Mon, 26 Jan 2015 17:16:57 -0800 Subject: [PATCH] 8069269: (spec) Defect in the System.nanoTime spec Change the description of how to compare two nanoTime values. Reviewed-by: martin --- .../share/classes/java/lang/System.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/jdk/src/java.base/share/classes/java/lang/System.java b/jdk/src/java.base/share/classes/java/lang/System.java index 1871774a022..ee6588491f9 100644 --- a/jdk/src/java.base/share/classes/java/lang/System.java +++ b/jdk/src/java.base/share/classes/java/lang/System.java @@ -376,19 +376,16 @@ public final class System { * the difference between two such values, obtained within the same * instance of a Java virtual machine, is computed. * - *

For example, to measure how long some code takes to execute: - *

 {@code
+     * 

For example, to measure how long some code takes to execute: + *

 {@code
      * long startTime = System.nanoTime();
      * // ... the code being measured ...
-     * long estimatedTime = System.nanoTime() - startTime;}
+ * long elapsedNanos = System.nanoTime() - startTime;}
* - *

To compare two nanoTime values - *

 {@code
-     * long t0 = System.nanoTime();
-     * ...
-     * long t1 = System.nanoTime();}
- * - * one should use {@code t1 - t0 < 0}, not {@code t1 < t0}, + *

To compare elapsed time against a timeout, use

 {@code
+     * if (System.nanoTime() - startTime >= timeoutNanos) ...}
+ * instead of
 {@code
+     * if (System.nanoTime() >= startTime + timeoutNanos) ...}
* because of the possibility of numerical overflow. * * @return the current value of the running Java Virtual Machine's