8188241: Improve numerical code in StampedLock example code
Reviewed-by: martin, psandoz, darcy
--- 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