8188241: Improve numerical code in StampedLock example code
authordl
Tue, 03 Oct 2017 13:32:04 -0700
changeset 47301 14a82b038e5a
parent 47300 a905ce398074
child 47302 f517fa4f4dc6
8188241: Improve numerical code in StampedLock example code Reviewed-by: martin, psandoz, darcy
src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java
--- a/src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java	Tue Oct 03 12:58:49 2017 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java	Tue Oct 03 13:32:04 2017 -0700
@@ -151,18 +151,20 @@
  *   }
  *
  *   double distanceFromOrigin() { // A read-only method
+ *     double currentX, currentY;
  *     long stamp = sl.tryOptimisticRead();
- *     double currentX = x, currentY = y;
- *     if (!sl.validate(stamp)) {
- *       stamp = sl.readLock();
+ *     do {
+ *       if (stamp == 0L)
+ *         stamp = sl.readLock();
  *       try {
+ *         // possibly racy reads
  *         currentX = x;
  *         currentY = y;
  *       } finally {
- *         sl.unlockRead(stamp);
+ *         stamp = sl.tryConvertToOptimisticRead(stamp);
  *       }
- *     }
- *     return Math.sqrt(currentX * currentX + currentY * currentY);
+ *     } while (stamp == 0);
+ *     return Math.hypot(currentX, currentY);
  *   }
  *
  *   void moveIfAtOrigin(double newX, double newY) { // upgrade