Created
June 26, 2020 19:39
-
-
Save searover/4f6c537f565a240c89647c1069971913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author luxz | |
* @date 2020/6/27-3:31 AM | |
* 如果执行乐观读操作期间,存在写操作,会把乐观读升级为悲观读锁 | |
*/ | |
public class Point { | |
private int x, y; | |
final StampedLock sl = new StampedLock(); | |
int distanceFromOrigin() { | |
// 乐观读 | |
long stamp = sl.tryOptimisticRead(); | |
// 读入局部变量,读入的过程中数据可能被修改 | |
int curX = x, curY = y; | |
// 判断执行读操作期间,是否存在写操作,如果存在,则sl.validate返回false | |
if (!sl.validate(stamp)) { | |
// 升级为悲观读锁 | |
stamp = sl.readLock(); | |
try { | |
curX = x; | |
curY = y; | |
} finally { | |
// 释放悲观读锁 | |
sl.unlockRead(stamp); | |
} | |
} | |
return (int) Math.sqrt(curX * curX + curY * curY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment