changeset 47301:14a82b038e5a

8188241: Improve numerical code in StampedLock example code Reviewed-by: martin, psandoz, darcy
author dl
date Tue, 03 Oct 2017 13:32:04 -0700
parents a905ce398074
children f517fa4f4dc6
files src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java
diffstat 1 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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